I have some pages on a website. All pages do have a bar, which I put with the following:
session_start();
include('bar.php');
echo $bar;
where the content of bar.php is more or less this:
session_start();
$username=$_SESSION['username'];
if empty($username) {
$bar='You are not logged in.';
} else {
$bar='You are logged in.';
}
The problem is that sometimes the bar works (i.e. it shows “logged in” when I actually am), but sometimes it doesn’t (it shows content for non-logged users). For example, browsing the index I see the bar for non-logged users, but going on the login page shows me the bar for logged users.
Also, I tried deleting my PHPSESSID cookie and then logging in: it worked, it didn’t show this bug. Why?
The second session_start(); simply is ignored, so I think this is not the problem. You could check if session_start() returns false: http://es.php.net/manual/en/function.session-start.php