group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
104 Members
Join this group to post and comment.
Shruthi Pillai
Shruthi Pillai
Information Technology
02 Feb 2019

How to use cookies in PHP?

Cookies are text files stored on the client computer and they are used for tracking purpose.  

Working of cookies

Server script sends a set of cookies to the browser. 

Browser stores this information on local machine for future use. 

When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.

Setting Cookies

PHP provided setcookie() function to set a cookie.  

Accessing cookies by $_COOKIE  

 isset() function is used  to check if a cookie is set or not



AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
2mos ago

A cookie is often used to identify a user.A cookie is a small file that the server embeds on the users computer.Each time the same computer requests a page with a browser it will send the cookie too.With PHP we can  both create and retrieve cookie values.It is created at server side and saved to client browser.

PHP setcookie() function:This function is used to set cookie with HTTP response.Once cookie is set we can access it by $_COOKIE superglobal variable.It is used to get cookie.

Syntax of setcookie-

Setcookie(name,value,expire, path,domain,secure,httponly);

Only the name parameter is required.All the other parameters are optional.

Example:$value=$_COOKIE["CookieName"]; //returns cookie value.

We can also delete a cookie.If we set the expiration date in past cookie will be deleted.

Example:

Setcookie("CookieName","",time()-3600); //set the expiration date to one hour ago.

?>

2mos ago

I request adding informative titles. One or two word titles will be deleted. I'm modifying the title of this this discussion.