Handling sessions with PHP

download Handling sessions with PHP

of 7

Transcript of Handling sessions with PHP

  • 7/27/2019 Handling sessions with PHP

    1/7

    Handling sessions with PHPHandling sessions with PHPHandling sessions with PHPHandling sessions with PHP

    Advanced Digital Media Technology Center-University of Colombo School of Computing

    Samantha Mathara Arachchi,B.Sc,Pg.Dip(Com.Tech.),Pg.Dip.(IM),M.Sc.(IM),MCS(SL) MACM,MIEEE

    e-mail:([email protected])

  • 7/27/2019 Handling sessions with PHP

    2/7

    Managing the "Memory" of the BrowserManaging the "Memory" of the BrowserManaging the "Memory" of the BrowserManaging the "Memory" of the Browser

    Web pages are normally completely without memory.

    Each time a person visits a page, the web server happily sends the page with no idea if thevisitor is viewing the page for the first time or the hundredth. Often times this is not a

    problem.

    Sometimes, however, it is a big problem, such as when you want to identify a user, in orderto interact with that person in various ways, even if he/she moves between differentwebpages.

    One method is to use cookies, which are totally dependent on the settings of the clientsweb browser.

  • 7/27/2019 Handling sessions with PHP

    3/7

    What is a session ?What is a session ?What is a session ?What is a session ?

    A session is a series of related interactions between a single client & a web

    server, taking place over an extended period of time.

    It is a method of making Data persist, even when you exit one page & go toanother.

  • 7/27/2019 Handling sessions with PHP

    4/7

    Session VariablesSession VariablesSession VariablesSession Variables

    How do we make the Browser remember a certain unit of data while we navigate

    from page to page? Simply, assign that data to a session variable, using thefollowing format - $_SESSION[ > ].

    eg. :- $_SESSION[stu_name] = Mr. ABC,

    $_SESSION[stu_id] = 8880123

    Here, stu_name and stu_id uniquely identify a variable.

    Another method is, to use the predefined function session_register( )

  • 7/27/2019 Handling sessions with PHP

    5/7

    Starting or Resuming SessionsStarting or Resuming SessionsStarting or Resuming SessionsStarting or Resuming Sessions

    A function called session_start() is used to start/create or resume a session in a

    particular webpage, based on the session id based on a GET or POST variable or acookie.

    This allows you to access the session variables input through a previous webpage.

    This is called initializing sessions.

    Two important points that have to be remembered about this function are,- Every sessions enabled page requires it.

    - You must put it before any HTML or PHP output (including blank linesor spaces).

  • 7/27/2019 Handling sessions with PHP

    6/7

    Stopping SessionsStopping SessionsStopping SessionsStopping Sessions

    Often times, you will need to destroy a session completely. This is a simple two step

    process:session_unset();session_destroy();

  • 7/27/2019 Handling sessions with PHP

    7/7

    Sample CodesSample CodesSample CodesSample Codes