Session and Cookies in PHP

Session

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

Example
<?php
session_start();
?>

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 :

<?php
unset($_SESSION[‘variable’]);
?>
Example to destroy all the session variables :

<?php
session_destroy();
?>

Cookies

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 *