Session and Cookies in PHP


Session is used to store information in a variable and access them multiple pages (If you want to make data accessible across the different pages on entire webpages it is possible by session). In PHP session stores data on the server rather than client computer.
Start Session In PHP
A session is started using session_start() function.

Session variables are set using PHP global variable $_SESSION


Destroy Session
In PHP session destroyed using session_destroy() function.If you want to destroy all the session variables at one time use session_destroy(). If you want to destroy a single session variable then you can use unset() function to unset a session variable.

Example of destroy for single variable session :

Example to destroy all the session variables :



Cookies are files used to stored on the client computer and kept for tracking purpose. PHP transparently supports HTTP cookies.

Setting Cookies in PHP
setcookie() function used to set a cookie. This function requires upto six arguments and should be called before <html> tag.

Leave a Reply

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