Sorry to butt in, but I just got finished doing the exact thing you're looking for 😉
Basically what I did was:
When the user logs in, it stores a bunch of session variables with information gathered from the database:
- "logged_id" = The userid (in my database) of the user.
- "logged_name" = The username they are logged in as.
- "logged_pass" = The md5 encrypted password for the user.
- "logged_in" = A flag. Either "yes" or "no".
Now, say someone logs in successfully and then visits a page that will have different content based on whether or not you they are logged in:
private.php:
session_start();
/* DATABASE VARIABLES */
$db = array (
"server" => "localhost",
"username" => "username",
"pass" => "password",
"db" => "database_name"
);
$dbconnect = mysql_pconnect($db["server"], $db["username"], $db["pass"]);
mysql_select_db($db["db"]);
/* Check to see if user has a session active.
If so, double check the registered session
variables against the database, cuz I'm paranoid. ;) */
if ($_SESSION["logged_in"] == "yes")
{
$query = "select * from users where username = '".$_SESSION["logged_name"]."'
&& password = '".$_SESSION["logged_pass"]."'";
$result = mysql_query($query, $dbconnect);
$valid_user = mysql_num_rows($result);
if ($valid_user != 1)
{
/* User has a session set, but the information stored in it
does not match with the database. Probably a lame
hacking attempt. Give em the "not logged in" content.*/
}
else
{
/* User has a session set and the information stored within
it DOES match the database. This is a valid user. Give
them the content valid users should see. */
}
}
else
{
/* User does not have a session established. Show them the
"not logged in" content and/or a login form. */
}
In the above example, one script can show two completely different results. This way if a valid user (one who has a session active) bookmarks the page, the next time they visit that page it will check for the valid session again. If the user has closed their browser since the bookmarking, the session has been destroyed and the page will display the "not logged in" content.
Hope this makes it clear. If not, feel free to email me with any questions: talon@thehellofit.com
Edited for formatting to prevent H-Scroll.