Fri Jun 12 2020

Cookie

PHP Scripting896 views

File Name: php-cookie.php

<?php

	/* Create cookie */	
	
	$username = "geek_user";
	
	/* Set cookie for localhost for 30 days */
	setcookie("user_name", $username, time() + (60 * 60 * 24 * 30), "/", "localhost"); 
	echo "Cookie created successfully!<br />";
	
	
		
	/* Count no of cookies are set */
	
	if(count($_COOKIE) > 0) 
		echo "There have ".count($_COOKIE)." cookies are set in your domain!<br />";
	else 
		echo "Cookie not yet set!<br />";
	
	
	
	/* Get cookie value */
	
	if(isset($_COOKIE['user_name']))
		echo "Cookie value is: ".$_COOKIE['user_name']."<br />";
	else 
		echo "Cookie not yet set!<br />";
	
		
		
	/* Modify cookie value and set for 2days (86400 = 1day) */
	
	setcookie("user_name","user_geek", time() + (86400 * 2), "/", "localhost");
	echo "Cookie modified successfully!<br />";
	
		
		
	/* Delete cookie */
	
	/* set cookie value null for less than a second */
	setcookie("user_name", null, time() - 1, "/", "localhost"); 
	
	/* Unset cookie */
	unset($_COOKIE['user_name']);
	echo "Cookie deleted successfully!<br />";
	
?>



/* Output */
Cookie created successfully!
There have 1 cookie are set in your domain!
Cookie value is: user_geek
Cookie modified successfully!
Cookie deleted successfully!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.