Beginners PHP Tutorial – Sessions
For users who prefer to learn PHP visually we have a range of video tutorials, this method of training greatly enhances learning and allows beginners to master PHP at their own pace.
View the Beginners PHP Video Tutorial
Beginners PHP Tutorials – PHP Sessions
PHP sessions are used to pass data between pages without having to encode that data and put it on the URL line. Sessions are different than cookies, in that sessions are not written to the user’s hard drive like cookies are, but they perform many of the same functions, to beginners PHP Sessions sound complicated but this tutorial shows the beginner just how simple Sessions are to use.
Sessions are safer to use than cookies because a user can block cookies from being written, but they cannot block sessions.
Like a cookie, sessions gives you a way to store data while the user is visiting the site. Unlike cookies, the data is not persistent. The session information is lost when the user leaves. Session data can be made persistent by storing it to a persistent cookie, or in your MySQL database, if desired.
How Sessions Work
When you create a session, PHP assigns a unique identification number to it. This is functionally equivalent to the way that a cookie assigns a cookie name. The unique session ID insures that one user’s data is not mixed in with another’s.
How to Initiate a Session
A session must be initiated at the top of your page before any other browser output. Otherwise an error will result and the session will fail. PHP provides the Session_Start() function for this purpose.
<?php session_start(); // start a new PHP session ?>
This is all it takes to register the new session with the server. Once this is done, you can begin storing information to the session variable.
How to Store a Session Variable
When you create a session, PHP initializes an associative array named $_SESSION. This is where you store and access session data.
<?php $_SESSION['UserID'] = ‘JoeGreen’; // store session data echo "Welcome “. $_SESSION[' UserID ']; //retrieve session data ?>
Advanced Session Use
You can use the PHP isset function to make sure that a session has been created and that any particular piece of session data has been set.
<?php if(isset($_SESSION['UserID'])) echo "Welcome “. $_SESSION[' UserID '];else echo ‘Please log in.’; ?>
The first time the user arrives at the site, the UserID session variable will not be set and the user will be directed to log in before they can continue to access the site.
Once they are logged in, the session variable will be present and they will see the welcome message instead.
Ending a Session
In the example above we used session data to track login status. If your script has a logout function, you would want to destroy the session data once the user logs out.
<?php if(isset($_SESSION['UserID'])) unset($_SESSION['UserID']); ?>
If you had other session variables as well, and you want to completely remove all of them, it is quicker to simply destroy the session.
<?php session_destroy(); ?>
Sessions can be used to track anything pertaining to a user. Combine the power of sessions with PHP’s excellent array functions and you could store an entire shopping cart’s worth of data with very little effort.