Using Session to Store Temporary Data
Sessions are a useful way to store temporary data, and even login information. In order to use sessions, the line
You can create a new session and initialize it using
A useful way of using Sessions is to see if a user has logged in (if you don't use cookies). After the user logs in, you can set the session using the above code. Later on, if you want to see if the user has logged in, you can do something like:
Sessions are stored on the server and are destroyed when the user closes their browser. Sessions can also be destroyed manually, i.e. when the user logs out. You can destroy a session by calling
session_start(); must be on the top of every PHP page you want sessions on.You can create a new session and initialize it using
$_SESSION['some_key'] = 'some_value';. You can retrieve the value of the session in a similar way: echo $_SESSION['some_key'];.A useful way of using Sessions is to see if a user has logged in (if you don't use cookies). After the user logs in, you can set the session using the above code. Later on, if you want to see if the user has logged in, you can do something like:
if ( isset( $_SESSION['user'] ) ) {
// session exists
// now check if session is valid
} else {
// ask user to login
}Sessions are stored on the server and are destroyed when the user closes their browser. Sessions can also be destroyed manually, i.e. when the user logs out. You can destroy a session by calling
session_destroy();, or a particular session value using unset( $_SESSION['some_key'] );. Destroying a session deletes all session data - so only destroy it if you don't need any of the temporary data.