What is Cookies and how to set and use Cookies in php ?

Cookie is the small piece of file that the server embedded in the user’s computer which is often used to identify the user thus it is a mechanism for storing the on the users browser and  identify the existing or returning users.

we can set a cookie using setcookie() function .

Declaration

setcookie(“Cookie name”, “value”,  ‘Expirey time ‘ , “path”  , “domain”, “secure” , “httponly” );
Example :
<?php 

setcookie(“Site User “, “Php Interview questions “, time() + (86400 * 30), “/”);

?>

You can simply check the cookie by  :


<?php
if(!isset($_COOKIE[‘Site User’])) {
    echo ‘Cookie is not set’;
} else {
    echo ‘Cookie is Set ‘;
    echo  $_COOKIE[‘Site User’] ; 
}
?>

To remove or delete the cookie , use setcookie() function in the expiration of past .

<?php
     setcookie(“Site User”, “”, time() – 3600);

  echo “Cookie is deleated “;
?>


For more Details : http://phpinterviewquestions.evernews.online/2018/02/what-is-difference-between-session-and.html

Leave a Reply

Your email address will not be published.Required fields are marked *