Persistent and Non-Persistent Cookie

Cookie used to store user data in form of text file or memory and access the values from it, Cookie are the part of HTTP header.

What is Persistent Cookie ?

Persistent Cookie which will store the data in text file with expire date and time. If you are access the website or particular page cookie will be created based on cookie name.  Next time If you are visiting the website or page it will be accessible from the cookie file which was created in the client browser.

Also Persistent Cookie is unsafe, because it will be easy viewable in the browser.

persistent_cookie.php

<?php
// creating the name:cookie_name_persistent and value:my cookie value with expire 1 hour
setcookie("cookie_name_persistent","my cookie value",time()+3600);

Above PHP file will create cookie and expire within 1 hour, you can view the cookie expire time in browser cookie part.

cookie_value.php

<?php
// checking the cookie_name_persistent cookie is created or not
if(isset($_COOKIE["cookie_name_persistent"])) {
	// accessing the cookie_name_persistent values from the cookie file
	echo "Persistent Cookie Value : ".$_COOKIE["cookie_name_persistent"];
}

What is Non-Persistent Cookie ?

Non-Persistent Cookie create in memory and access from it, If you close the browser the cookie will be deleted automatically.  So next time if you are visiting the website the cookie won’t be exists because it not stored in the text file. So Non-Persistent Cookie are safer than the Persistent Cookie.

non_persistent_cookie.php

<?php
// creating the name:cookie_name_non_persistent and value:my cookie value without expire time
setcookie("cookie_name_non_persistent","my cookie value");

cookie_value.php

<?php
// checking the cookie_name_non_persistent cookie is created or not
if(isset($_COOKIE["cookie_name_non_persistent"])) {
	// accessing the cookie_name_non_persistent values from the memory
	echo "Non Persistent Cookie Value : ".$_COOKIE["cookie_name_non_persistent"];
}