Forum Moderators: coopster
I'm trying to get the sign in / log out link to change depending on whether the user is logged in or out. The login form action script starts the session and creates a cookie, it looks like this:
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
session_start();
$username = $_POST["username"];
$userpass = md5($_POST["userpass"]);
$sql = "SELECT username , userpass FROM user WHERE username='$username' AND userpass='$userpass'";
$result = mysql_query($sql);
if (mysql_num_rows($result)!= 1) {
$loginError = 1;
include "login.php";
} else {
$_SESSION['username'] = "$username";
setcookie("member", "logged", time()+1800);
header("Location: profile.php");
}
?>
Then, we have the sign in / log out section found on every page:
<li><?php if(!isset($_SESSION['username'])) echo '<a href="../../ac/login.php">Sign In</a>'; else echo '<a href="../../ac/logout_action.php">Log Out</a>'; ?></li>
So, I'm stating that if the session is not existent then sign in will show otherwise show log out. It works fine on profile.php but it will not work on any other page. I have PHP set to show all errors and no errors are showing up, so it's just not detecting the session. I checked through firefox and both the cookie and session exist. I even tried the following:
<?php if(!isset($_COOKIE['member'])) echo '<a href="../../ac/login.php">Sign In</a>'; else echo '<a href="../../ac/logout_action.php">Log Out</a>'; ?>
And still, it will only work on profile.php and not any other page. Any suggestions?
Thanks,
Max