Tuesday, August 12, 2008

Using session in PHP

Keeping in mind the fact that Internet is a stateless platform and every request for a web page is treated as unique, there is a serious need of a tool to maintain the state. Otherwise, it will be a messy situation to keep track of requests made by a particular user. The good news is that use of PHP session serves the solution to this problem. This session variable is of great importance for web applications like shopping carts and is considered as equivalent to cookies, other significant way of maintaining the state.

What Is PHP Session Capable Of

PHP session is capable of storing the information in the form of session variables, in order to handle the ever-increasing traffic on a website. For instance, consider a shopping website, with products scattered in different categories and different pages. In such a situation, a user may switch from one page t another and keep on adding products from different pages to his or her shopping cart.

Thus, in order to keep the track of a particular user on the shopping website, PHP sessions store user information and use it again and again. This is a powerful entity to serve the purpose for websites with huge customer base.

Getting Started With PHP Session

PHP session is initiated using following piece of code:

1.
session_start();?>

Now, suppose you visit a website in regular intervals in a specific period of time. The website must not treat you like a new user on every visit of yours. Rather, it must take the advantage of session variable to assign a user value to your first visit and use it again and again for every visit paid by you. Thus, we can add following code to accomplish this task:

1.
session_start();$_SESSION[‘counter’]++;
2.

3.
echo "Welcome Back! You have viewed this page " . $_SESSION[‘counter’] . " times";
4.

5.
?>



Thus, if you revert to a particular website in short period of time, the counter will be incremented. If you visit the website 5th time, you might see it written somewhere that “Welcome Back! You have viewed this page 5 times”.

How Does It Work

Whenever you visit a web page, the PHP session sets information on your computer in the form of a cookie. It is generally a random key, formed by alphabets and numbers. In case, you visit another webpage of same website, the session explores the computer for a previously generated key. In case a match is found, that particular session is utilized. Otherwise, a new session is created with whole procedure related.

How To End A PHP Session

A session is destroyed with the help of following code:

1.
session_destroy();
2.

3.
?>

It is a matter of fact that a session exists until and unless the associated user closes the browser used to open the website. Once the browser window is shut down, the session is destroyed. You may also decide the life of a session by bringing in following modifications in php.ini file.

session.cookie_lifetime = 0

You may change the value from zero to number of seconds you want a session to live.

You may also destroy a session variable using unset() or all session variable using session_unset().

No comments: