Back to Top

How to Use PHP Sessions to Store Data

How to Use PHP Sessions to Store Data

PHP Session is a very important as we generally use the session to maintain data throughout the application in temporary form.The session can be used to store various types of data so it has many uses, also used to store the login information of the user, shopping cart data, or some temporary data.

What is a Session?

The session is the variables which is used to move data between pages of hiding from the others.A PHP session allows an application to store information for the current session.A session is identified by a unique session ID.The session is used to store data temporarily.

Session Usage

The first step in a script that is to let PHP know you want to use the session feature by calling the function session_start() which takes no argument.

So, To initialize the session with session_start() at the beginning of the page to use session:

session_start();

To create the session,We need to use $_SESSION array.The $_SESSION array is one of the superglobal variables. We do not need a function or class. You can use the $_SESSION superglobal array which will store anything that you want to retrieve again in any page of application in the same session,we can do here.

How to get the Session Value

Again, using the $ _SESSION superglobal array in our page. index.php page will direct you to our page.

The code can be as simple as the following example:

Now,Lets see the index.php code:

Although,you can get the session in different files on different pages without the knowledge of the browser version from the array name of $_SESSION.The $_SESSION array is a way of sharing information between pages.

How to delete Session

There are many ways to delete or destroy your session.First, let’s use the unset function.

  1. unset()

The unset function is very easy to remove from the array, just like other arrays.

  1. session_destroy()

If you want to delete all the elements in the array,session_destroy() is useful.session_destroy() function clear the session by setting the $_SESSION superglobal to an empty array as shown in the following example:

A session ID of browser may still be the same after this function call so if you call session_start() again,you may get value again so it is better to use session_regenerate_id() after destroying session:

  1. session_unset():

session_unset() function takes no argument and release all variables in the session

  1. $_SESSION = array();

How to Update Session

Process we perform to update the normal array, the same way you can update to session array.

It’s that simple. That’s all there is to it.How are you using session in your PHP application? I’d love to hear about your experiences in the comments.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Function handling Functions in PHP

Posted on 12 years ago

Bhumi