0% found this document useful (0 votes)
150 views3 pages

PHP Cookie

PHP cookies are small pieces of information stored in a user's browser by the server. Cookies are created on the server side and sent to the browser, where they are stored and sent back to the server with each subsequent request. The PHP setcookie() function is used to set a cookie, specifying its name, value, and optional attributes like expiration and path. Cookies are accessed on the server side through the $_COOKIE superglobal array. Cookies can be deleted by setting the expiration date in the past.

Uploaded by

SourabH BhalsE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views3 pages

PHP Cookie

PHP cookies are small pieces of information stored in a user's browser by the server. Cookies are created on the server side and sent to the browser, where they are stored and sent back to the server with each subsequent request. The PHP setcookie() function is used to set a cookie, specifying its name, value, and optional attributes like expiration and path. Cookies are accessed on the server side through the $_COOKIE superglobal array. Cookies can be deleted by setting the expiration date in the past.

Uploaded by

SourabH BhalsE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP Cookie

PHP cookie is a small piece of information which is stored at client browser.


It is used to recognize the user.

Cookie is created at server side and saved to client browser. Each time when
client sends request to the server, cookie is embedded with request. Such
way, cookie can be received at the server side.

In short, cookie can be created, sent and received at server end.

⮚ PHPCookiemust be used before <html>tag.

PHP setcookie() function


PHP setcookie() function is used to set cookie with HTTP response. Once
cookie is set, you can access it by $_COOKIE superglobal variable.

Syntax

1. bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path  
 
2. [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )  

Example

1. setcookie("CookieName", "CookieValue");/* defining name and value only*/ 
 
2. setcookie("CookieName", "CookieValue", time()+1*60*60);//using expiry in 
1 hour(1*60*60 seconds or 3600 seconds)  
3. setcookie("CookieName", "CookieValue", time()+1*60*60, "/mypath/", "my
domain.com", 1);  

PHP $_COOKIE
PHP $_COOKIE superglobal variable is used to get cookie.

Example

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

PHP Cookie Example


File: cookie1.php
<?php  
setcookie("user", "Sonoo");  
?>  
<html>  
<body>  
<?php  
if(!isset($_COOKIE["user"])) {  
    echo "Sorry, cookie is not found!";  
} else {  
    echo "<br/>Cookie Value: " . $_COOKIE["user"];  
}  
?>  
</body>  
</html>  

Output:

Sorry, cookie is not found!

Firstly cookie is not set. But, if you refresh the page, you will see cookie is
set now.
Output:
Cookie Value: Sonoo

PHP Delete Cookie


If you set the expiration date in past, cookie will be deleted.
File: cookie1.php
<?php  
setcookie ("CookieName", "", time() - 3600);// set the expiration date to one 
hour ago  
?>  

You might also like