What are HTTP cookies? How do you handle them in PHP?

What are HTTP cookies? How do you handle them in PHP?


What are HTTP cookies? How do you handle them in PHP?

Answer

  • A cookie is a small file that the server embeds on the user's computer.
  • Each time the same computer requests a page with a browser, it will send the cookie too.
  • With PHP we can create and retrieve cookie values.

Creating Cookies with PHP


A cookie is created with the setcookie() function.

Syntax
setcookie(name,value,expire,path,domain,secure,httponly);

Only the name parameter is compulsory all other parameter is optional.

Accessing Cookies with PHP

We can access cookie through many ways the simplest way is use either $_COOKIE
or $HTTP_COOKIE_VARS variables

Example of Setting Cookie and Accessing it.

<?php
setcookie("name","GetWays",time()+100,"getwayssolution.com/",0); #setting the name cookie
setcookie("age","36",time()+100."getwayssolution.com/",0);  #setting the age cookie
?>
<html>
<head></head>
<body>
<?php
echo $_COOKIE["name"]."<br>"; #accessing name cookie
echo $_COOKIE["age"]."<br>";    #accessing age cookie
</body>
</html>

Other Question With Answer


















No comments:

For Query and doubts!

Powered by Blogger.