PHP Cookies

A cookie is often used to identify a user. 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, you can both create and retrieve cookie values.

Create Cookies

setcookie() method contains different parameters but in the below example we will only use 4 parameters, Cookie Name, Cookie Value, Time in Seconds and Path.


<?php

	$cookie_name="color";
	$cookie_value="black";
	
	setcookie($cookie_name,$cookie_value,time()+86400,"/");
	
?>

Read Cookie

As php have different Global arrays like $_POST, $_GET and etc. same PHP contains $_COOKIE array to access/set cookies values.


<?php

	//read the cookie data
	if(isset($_COOKIE['color'])){
		echo $_COOKIE['color'];
	}else{
		echo 'No Cookie Found';
	}
?>