PHP Cookies Use, Application in Example Code to Set, Delete, setcookie

PHP Cookies

By default PHP is a stateless application means you can not track the activity of a user during the accessing of different web pages of the web site. With the help of the cookie, you can set some variable to track the user state.

PHP cookie is a Super Global and used to set some variable to track the user state or activity.

Caution: Cookie variables are stored in the client or visitor computer, not on the web server.

PHP Setcookie Function to set cookie in PHP

With the setcookie() function you can create a cookie.

Syntax of the setcookie() function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
		setcookie(name, value, expire, path, domain, secure, httponly);
		
   </body>
  </body>
</html>

Note:In setcookie() function name parameter is required others parameters are optional.

Example – 1 Code to set and read Cookies

<?php
$cookie_name = "visitor";
$cookie_value = "John William";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		if(!isset($_COOKIE[$cookie_name])) 
		{
			echo "Cookie named '" . $cookie_name . "' is not set!";
		} 
		else 
		{
			echo "Cookie '" . $cookie_name . "' is set!<br>";
			echo "visitor = " . $_COOKIE[$cookie_name];
		}
?>
  </body>
</html>

In the above example, we create a cookie of name visitor and the value is John William”

Note:You must have to write setcookie() function code before the starting of HTML tags.

Set Cookie in PHP
Fig.1 – Set Cookie Example code with variable name “visitor”.

How to Modify a Cookie Value

If you want to modify the value of the cookie again then you have to set the value of cookie through setcookie() function again the value will be modified.

Example – 2 To modify the Cookie Value code

<?php
$cookie_name = "visitor";
$cookie_value = "Lariya Ela";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		if(!isset($_COOKIE[$cookie_name])) 
		{
			echo "Cookie named '" . $cookie_name . "' is not set!";
		} 
		else 
		{
			echo "Cookie '" . $cookie_name . "' is set!<br>";
			echo "visitor = " . $_COOKIE[$cookie_name];
		}
?>
  </body>
</html>

In the above example, we create a cookie of name visitor and now I change the value of a cookie with “Lariya Ela”.

PHP Setcookie
Fig.2 – Modification of Cookie Value.

How to Delete Cookie in PHP

If you want to delete the setcookie then you have to set the cookie again but the time will be set in negative or past.

Example – 3 Delete Cookie Code in PHP

<?php
setcookie("visitor", "", time() - 4500);
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		
		echo "The cookie visitor deleted";
?>
  </body>
</html>

In the above example, we delete a cookie of name visitor.

Delete Cookie PHP
Fig.3 – Delete Cookie PHP Example Code.

To know the Cookie is Enabled or Not in the User Computer

If you want to get information about the cookies are enabled. The below code is used to know the information about the cookies are enabled or not.

Example – 4 Check Cookie is Enable or Not Code

<?php
setcookie("visitor", "", time() + 3600);
?>
<!DOCTYPE html>
<html>
  <head>
    
  </head>
   
  <body>
    <?php
		if(count($_COOKIE) > 0) 
		{
			echo "Cookies are enabled.";
		}
		else 
		{
			echo "Cookies are disabled.";
		}
	?>
  </body>
</html>
PHP Cookies Example
Fig.4 – Check Cookies Enable or Not.
You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Company
Popular Learning
Reach To Us
Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!

Powered by Wordpress