A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.
So; Session variables hold information about one single user, and are available to all pages in one application.
Create a PHP Session
A session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.
Put this code in a test.php file and load this file many times to see the result –
// Start the session
// Set session variables
echo"Session variables are set.";
It will produce the following result –
Session variables are set.
Note: The session_start() function must be the very first thing in your document. Before any HTML tags.
Cookies vs Sessions
Both cookies and sessions are used for storing persistent data. But there are differences for sure.
Sessions are stored on server side. Cookies are on the client side.
Sessions are closed when the user closes his browser. For cookies, you can set time that when it will be expired.
Sessions are safe that cookies. Because, since stored on client’s computer, there are ways to modify or manipulate cookies.
Destroying a PHP Session
A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
Vinay Bobade (Digital Technology Hub) is an excited individual who is enthusiastic for his work. He is anxious to investigate new things other than composing online journals which contain supportive data with respect to IT arrangements and programming administrations. Aside from composing, he is inquiring about on Computerized Innovation industry arrangements