First off, I never store an unencrypted passwords in sessions, or any password if I can get around it which usually isn't that difficult.
I am in the habbit of registering an array as the session var the stuffing everything into that. Less chance of forgetting to unset something. Plus easier if you need to pass a full array from a database, such as user info, into the session var. In the example below the session var array is $DATA and everything else is an associative member of it (ie $DATA[username])
Note that this code isn't tested so may need some tweaking...
<?
session_start();
IF($user!="" && $pwd!="") //
{
$connect = mysql_connect();
mysql_select_db("x");
$query = mysql_query("SELECT * FROM the_user_table WHERE (user='$user' AND pwd='$pwd'");
IF($mysql_num_rows($query)==1)
{
$row = mysql_fetch_array($query);
$DATA=array("user"=>$row);
$DATA["user"]["pwd"]=md5($pwd);
session_register("DATA");
}
mysql_close($connect);
header ("Location: main.php")
}
ELSE
{
print "Incorrect Login. Please try again.";
}
?>
-- main.php: --
<?
session_start();
if ($HTTP_SESSION_VARS["DATA"]["user"])
{
// Authenticated
?><A href="logout.php">Log out</A><BR><?
}
else
{
// Auth failed
header("location:.")
}
-- logout.php --
<?
session_start();
session_unregister("DATA");
session_destroy();
print ("Logged out.<BR>");
print ("<A href=\"index.php\">Log in again</A>");
?>