Question:

What are session variables and how are they used in PHP programming?

by  |  earlier

0 LIKES UnLike

What are session variables and how are they used in PHP programming?

 Tags:

   Report

3 ANSWERS


  1. Session variables are temporary variables stored in the browsers temporary files. It can contain information such as session id, usernames, passwords, etc. They are used to create login systems. Their purpose is to keep the user logged in.

    To set a session variable in PHP, you must put the following BEFORE any HTML in the page.

    <?php

    session_start();

    $_SESSION['session_name'] = "session_data";

    ?>

    The information will remain there until the browser is closed.


  2. they make e-commerce sites and social networking sites possible. actually, any site that requires "remembering" you, needs session variables. they are an evolution of cookies, which save your information and are used when you return to certain websites.

    the internet is inheritantly "state-less", meaning you go to a webpage and upon loading the next webpage, the server that sent you the first webpage completely forgets you. It serves you the page, then goes away, ready to be dispatched when you request another page. No data is stored, no recollection of your visit (other than your own browser history or server hit data). By instituing a session, the server "remembers" your information as you go from page to page, perfect for an application like a shopping cart.

    Imagine having to shop online with one web page. All the products would have to be on one page, all the checkout info and shipping information also on the same page. Without sessions, this is what would be needed to work. So when you're building a site and require a user login or profile, sessions are vital to making it work.

  3. session variables referenced by $_SESSION['var_name'] are variables that stored between request and responses between the server and client.  Since php is what is referred to as a stateless language, meaning that it runs left to right top to bottom and then ends with responding to the client, all those variables and objects that were created are gone after the script finishes.  However when a client requests something from the server, the php runtime, called the Zend engine, sends a cookie back to the browser that is a unique id.  The php system registers the session id and registers any values you set in it.  So that when the client requests another page from the server, the cookie that was set is sent as well.  The php subsystem reads this value and finds all the information that your scripts registered with it,  This seems to create the illusion that the script never ended and the important values are still available.  When the user closes their browser, that cookie that the php server sent is deleted and if they try to request something from the same server again, there is no session id cookie sent, therefore the session is broken.  Hope this helps.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions