Hi,
I'm having a slight problem on my home page. Depending on if the user is logged in or not depends on what navbar they see. However, even if the user is logged in - it will sometimes tell the user he is logged out. If you refresh the page then it will show the correct status. If you go to another page then come back, it will then show the correct status.
Even if it does show correctly that the user is logged in, if you refresh the page 4 or 5 times it will then show that he is logged out, refresh again and bam he's logged in. It seems as though it's completely random.
Here's the code from the pages:
Homepage:
<?php
require('functions/startsession.php');
require('functions/header.php');
$page_title = 'Welcome Home';
require('functions/footer.php');
?>
Session Start:
<?php
session_start();
// If the session vars aren't set, try to set them with a cookie
if (!isset($_SESSION['user_id'])) {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
}
}
?>
Header:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
echo '<title>Site Name | ' . $page_title . '</title>';
?>
<link rel="stylesheet" type="text/css" href="formstyle.css" />
<link href="functions/stylesheet.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
</head>
<body class="thrColFixHdr">
<div id="container">
<div id="header">
<img src="images/logo.gif" width="1024" height="80" alt="logo" longdesc="index.php" />
<?php
require('navmenu.php');
?>
<br />
<!-- end #header --></div>
<div id="sidebar1">
<h3>Content</h3>
<p>Content inserted here, no scripts</p>
<!-- end #sidebar1 --></div>
<div id="mainContent">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="middle">
<?php
echo '<h3>' . $page_title . '</h3>';
?>
</td>
<td align="right" valign="middle">
<div class="welcome">
Welcome,
<?php
if (isset($_SESSION['username'])) {
echo $_SESSION['username'];
?>
<p class="italics">Not you? Please <a href="authentication/logout.php">log out</a>.</p>
<?php
}
else {
?>
Guest
<?php
}
?>
</div>
</td>
</tr>
</table>
Nav Menu:
<ul id="MenuBar1" class="MenuBarHorizontal">
<li><a href="index.php">Home</a></li>
<?php
if (isset($_SESSION['user_id'])) {
?>
<li><a href="profiles/search.php">Search</a></li>
<li><a href="mail/inbox.php">Mail</a></li>
<li><a href="register/settings.php" class="MenuBarItemSubmenu">Account</a>
<ul>
<li><a href="profiles/edit_profile.php">Edit Profile</a></li>
<li><a href="authentication/change_password.php">Change Password</a></li>
<li><a href="register/settings.php">Settings</a></li>
</ul>
</li>
<li><a href="authentication/logout.php">Logout</a></li>
<?php
}
if (!isset($_SESSION['user_id'])) {
?>
<li><a href="register/signup.php">Sign Up</a></li>
<li><a href="authentication/login.php">Log In</a></li>
<?php
}
?>
</ul>
Thanks and any help would be greatly appreciated.