Sessions

When you define a variable or function or whatever, it is good for as long as the program runs (which is a blink of an eye). The PHP parser receives code, it processes it and sends it back as HTML. While the parser is open and processing your code, everything you create is there for use. When the parser closes your program and sends HTML back to the server, nothing you created exists any longer.

How, then, can you define a variable, or function or whatever on one page, and have it available on another page? PHP provides several ways to make data persist from one page to another and all of them include storing the data in some way and recalling it when the new page opens.

USING SESSIONS

The session identifier (SID) is a constant that identifies any one session. When a session is started, the server will assign a SID. You can then register session variables that will be stored on the server. To access the session variables, the client has only to include the SID in their request to the server. The built-in function session_id() will display the SID value.

If cookies are enabled on the client machine, the server will send the SID to a cookie on the client machine, so it is available. As with all cookies, it will disappear after a certain amount of time or when the client's browser closes. If you can not rely on enabled cookies, you must go to Plan B and use hidden form fields (<input type='hidden' name='SID' value='<?= session_id(); ?>' />) or a query string (<a href='page2.php?SID=<?= session_id(); ?>'>page 2</a>) to pass the SID manually. You then retrieve it with $_REQUEST['SID'] on page 2.

You must start/renew a session before any HTML has been processed. This means you must put the accompanying code before any HTML, including any blank spaces.

To start a session, use session_start(). If this is the first page, a SID will be created automatically (or, if you prefer, you can create your own SID with session_id()). Hereafter you must pass SID to subsequent pages. On the subsequent pages, you still use session_start() to make the session variables available, but with SID passed, a new one is not created.

To register variables, simply instantiate $_SESSION as you would any other array: $_SESSION['lastName'] = "Smith";, $_SESSION['rank'] = "TSgt"; and so on.

That's all there is to it. To make the following example work, first open the last lesson (RegEx), then navigate to this page from that page (by clicking on an appropriate link). If you navigated to this page from the last lesson already, then you need not do it again. Here is an example:

<?php session_start(); ?> ... later in the file ... <?php $_SESSION['MIL_ID'] = "wsanders"; $_SESSION['user_age'] = 72; $pData = array('rank'=>'LtCol','lastName'=>'Sanders'); $_SESSION['pData'] = $pData; ?>

(I put this in the previous lesson--RegEx.) Now, on a subsequent page, you can do like so:

<?php session_start(); ?> ... later in the file ... The instructor for this class is <?= $_SESSION['pData']['rank']." ".$_SESSION['pData']['lastName']; ?>. He is only <?= $_SESSION['user_age']; ?> years old.

(I put this in this page. If you go to the RegEx lesson first, then come back to this one, the text will appear as intended below.) This will produce:

The instructor for this class is . He is only years old.

Note: if you don't put session_start(); at the top of any subsequent page, the registered variables will not be available. Also, the above assumes cookies are enabled; if not, use hidden form fields or query strings as described above to pass 'SID'.


Regular Expressions   < <  PREVIOUS   Table of Contents NEXT  > >   Sending Emails

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,586 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHP12.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 10:53 pm, UTC
This file took 0.00987 seconds to process.