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.
- Hidden Form Fields
- if you need to send data from a form, include hidden form fields
- <input type='hidden' name='goodSite' value='Sandersongs' />
- anyone who does a 'view source' will see this
- Query Strings
- define your variables/functions/etc in a in a query string appended to a URL
- <a href='page2.php?goodSite=Sandersongs'>page 2</a>
- anyone who looks at the address bar on his browser will see the values
- define your variables/functions/etc in a file (called an include file)
- call this file for any other file that needs it
- Flat Files
- store your data in a ASCII (text, not binary) file (commonly a *.txt file)
- use PHP to open the file, and read it or write data to it
- store your data in a database
- use PHP to retrieve data from the database or store data in it
- store your data in a cookie on the client machine
- use PHP to read the cookie and write new cookies
- store your data in a session on the server
- use PHP to read the session and write new data in the session
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:
(I put this in the previous lesson--RegEx.) Now, on a subsequent page, you can do like so:
(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:
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'.