You may start a "session", once started, you may use the
super-global array $_SESSION much like the others you know, e.g. $_GET.
The difference is that you can store values in this array, PHP will
save them and they will be available to subsequent pages. The session
(for each client) is identified by a cookie stored by the client, and
it remains valid until the client's browser is closed, just like a
normal cookie.
The session data, on the other hand, is stored at the server. [Ah,
it is subject to "garbage collection" if unchanged for over 24 minutes.]
session_start();
$_SESSION['name'] = $_GET['name'];
$_SESSION['favcolor'] = 'green';
$_SESSION['time'] = time();
echo '<br />Go on to <a href="session2.php">page 2</a>';
Note that the link to session2.php has no query string. Yet, it can
retrieve the session data:
session_start();
$name = $_SESSION['name'];
echo "Welcome to page #2 $name<br />";
echo '<br>Our favorite colour: ';
echo $_SESSION['favcolor']; // green
echo '<br>Your session started: ';
echo date('Y m d H:i:s', $_SESSION['time']);
Many of the details of how sessions work are specified in a system file php.ini. These variables can be overridden using ini_set(). In particular:
session.cookie_lifetime = | "0" | Seconds until expires (0=until
browser closes) |
session.cookie_path = | "/" | Default, all of server |
Now, on Osiris we do not want to interfere with one another's
settings, so I have changed the default there to "", so the session
will be limited to the directory in which the session is first started,
and any subdirectories. Should you already be in a subdirectory when
starting, you can override the path so it includes all your pages, as
follows:
ini_set('session.cookie_path','/youracct');
See the PHP documentation, session reference, for the complete list.
Be aware that the cookie_lifetime is from its creation, so the default
"0" is probably the safest.