Web Info and IT Lessons Blog...

Thursday 29 January 2015

Session in PHP

The previous lesson of PHP Lessons Series was about include and require and in this lesson we will learn about PHP Sessions.

PHP Sessions

Session is a way of storing information using session variables to be accessed anywhere in a website. Unlike a cookie, session data is stored on server side and not on the user's computer. A variable in PHP retains it's last modified value till the page is not refreshed but this is not true for a session variable. You can refresh your page many times without losing the last modified value stored in your session variable. When a session variable is set, it is globally available to you anywhere in your project.

Use of Session:

Usually sessions are used on websites which require the user to be logged in. Once a user enters the login information, it is stored in the session variable and is available to be used anywhere in the website until the session is destroyed.

How to start a PHP session?

Starting a session in PHP is very simple. All you have to do is call the session_start() function at the top of your php file and then set the session variable.


session_start();

$_SESSION['name'] = 'John';
$_SESSION['email'] = 'john@gmail.com';

The above code starts a session and set the session variables.

How to access the session values in PHP?

To access the values of session variables that were set in other files of project in your current file, all you have to do is start the session at the top of your current file by calling session_start() function and use the session variables.


session_start();

$name = $_SESSION['name'];


In the above code we have started a session in our current file to access the session variable ($_SESSION['name']) that was set on a different file. One important thing to notice here is that whenever we need to access the session values, we have to start a session in our file first.

If you want to know which session variables are set, the best thing to do is print the session array using print_r statement.


session_start();

print_r($_SESSION);


The above code will print all the session variables set.

How to end session in PHP?

There are three methods of ending a session in PHP.

1. unset
2. session_unset
3. session_destroy

unset in PHP
unset function is used to end a single session. The basic structure of unset is given below:


unset(sesson_variable);

In unset function we have to pass a session variable that we want to end. Forexample if we want to end the session ($_SESSION['name']), all we have to do is pass it in the unset function.


unset($_SESSION['name']);

session_unset in PHP
session_unset is used to destroy the session and unset all the session varaibles. The basic structure of session_unset is given below:


session_unset();

session_unset() function takes no parameter as it has to destroy all the sessions.

session_destroy in PHP
session_destroy is used to destroy a session but the session variable set can still be accessed on the current page and not on any other page. The basic structure of session_destroy is given below:


session_destroy();

session_destroy() function does not take any parameter.

When do we need to destroy a session?
The need to destroy a session arises whenever we need to log a user out and we want our website to forget about the user information we had stored earlier in our session variables. Destroying the session will automatically log the user out.

Related Posts
Cookies in PHP
Get and Post methods in PHP
Include and Require Statements in PHP
How to get the ip address of user in PHP?

No comments:

Post a Comment