Hi,
I haven't got an example to hand, but the basic idea is as follows:
In the login script:
Use session_start() to begin a session.
when the user has input his/her user and password, save the userid to a session variable. You can do this using either
$_SESSION['userid'] = $userid_from_form;
OR (you shouldn't mix and match these - the first one is preferable)
$userid = $userid_from_form;
session_register('userid');
The session_start creates a unique id which is stored in a cookie or is passed in the url depending on browser set-up. The session_register or assignment using $_SESSION will create a variable and place it's name and value into the session file. If it's a new session then the session file is automatically created for you.
Obviously the user name/password should be checked with the database before assigning the session variable to indicate that the user is logged on.
You should place a session_start() in any scripts that need access to the variables ! This then re-assigns the variables from the session file (who's name is related to the unique id in the cookie/url).
When you need to authenticate that the user has logged on, simply check that the session variable is set. Use either:
if (isset($_SESSION['userid'))
{
.........
.........
}
OR (use whichever you used before)
if (session_is_registered('userid'))
{
.........
.........
}
if it is set, then the user is logged on. You can then use the userid to read the database for the user's details.
The session will (might) time out eventually - depending on your PHP configuration settings. So if you find that the session variable isn't set you can redirect your visitor to the log on screen again.
Having said all that - I'm struggling to get sessions working at the moment, so I may not be the best one to ask. Sessions can go further and use other methods for storing the variables, you can find that in the manual (www.php.net).
Cheers,
Nick.