i think sessions are better... cookies someone could actually go in and read while they are stored on your cpu.. not likely, but possibly.... sessions are great.. but the user has to logout because i have noticed that sometimes the SESSIONID is still a link in the browser.... but.. the logout fixes that... correct?.. anwho.... to the code.. this worked well for me.. basic.. and does what you are asking for
on your login page
<?
session_start();
if ($user && $pass)
{
// if the user has just tried to log in
$db_conn = mysql_connect("localhost", "username", "pword");
mysql_select_db("databasename");
$query = "select * from tablenamegoeshere where name='$user' and pass='$pass'";
$result = mysql_query($query);
if (mysql_num_rows($result) >0 )
{
// if they are in the database register the user id
$valid_user = $user;
session_register("valid_user");
}
$goto = header ("Location: [url]http://www.locationyouaregoingto.com[/url]");
}
?>
i also put this in the middle of my page to display whether or not they are logged in..
<?
if (session_is_registered("valid_user"))
{
echo "$goto";
}
else
{
if (isset($user))
{
// if they've tried and failed to log in
echo "Could not log you in";
}
else
{
// they have not tried to log in yet or have logged out
echo "You are not logged in.<br>";
}
}
?>
members page or page you directed to.. at the top goes...
<?
session_start();
?>
then in the middle that page it displays that you are logged in as... username... and if not it says you are not.. and that code is here.....
<?
// check session variable
if (session_is_registered("valid_user"))
{
echo "You are logged in as: $valid_user <br>";
}
else
{
echo "<p>You are not logged in.<br>";
}
?>
to logout.. you need to make this page..... and send link it to a logout button.. which will destroy the session...
<?
session_start();
$old_user = $valid_user; // store to test if they *were* logged in
$result = session_unregister("valid_user");
session_destroy();
?>
then again...you can echo out like above.. if they are logged in or not...
hope that helps...