$_SESSION in PHP. How create, use, remove. Code example open

$_SESSION in PHP. How create, use, remove. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

Sessions are a set of variables that are stored on the server. To start a session, you need to call the session_start() function.
The session allows you to save the state between different requests and interact with the user over several pages.
It must be called before the response is sent to the user:

session_start();//start the session before the call
$_SESSION['login'] = 'admin';//create a new element of the array

You can refer to a variable to handle this data $_SESSION

echo $_SESSION['login'];

To operate on the data of a variable on other page it is necessary to start session:

session_start();

To remove an array element from a session, use the unset() function

unset($_SESSION['login']);

Get the session ID

php session_start();
echo session_id(); // session ID
echo session_name(); // session name 

You can delete all session data using the session_destroy() function:

session_start();
session_destroy()
Note that to use $_SESSION, you must call session_start() at the beginning of every script that uses a session. This allows PHP to establish or restore a session for the current user.
0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation