problem with log in code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

problem with log in code

Post by beginner123 »

I am making a website where users can create an account and sign in. So in the website is a userbar with says: 'sign in or create account'
Everything is working fine accept after a user logs in, the userbar should change to 'Hello (then username)' but it doesn't.
The userbar stays as 'sign in or create account' even though the user is signed in. If i click sign in again a message appears saying already signed in.

here is my code for the userbar:

Code: Select all

   <?php
	$host = "localhost";
	$username = "root";
	$password = "";
	$database = "k00127082"; 

//error with connection
if(!mysql_connect($host, $username, $password))
{
 	exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
 	exit('Error: could not select the database');
}
		if(isset($_SESSION['signed_in']) == true)
		{
			echo 'Hello <b>' . ($_SESSION['userName']) . '</b>. <a href="signout.php">Sign out</a>';
		}
		else
		{
			echo '<a href="signin.php">Sign in</a> or <a href="signup.php">create account</a>';
		}
		?>



and then in all my other pages i include this code to display the userbar:

Code: Select all

<div id="userbar">
                <?php
                include 'userbar.php';
                ?>
           </div>
i know the problem is with the if statement - this line doesn't work if(isset($_SESSION['signed_in']) == true)
it keeps jumping into the else
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: problem with log in code

Post by Mordred »

Call session_start() before accessing $_SESSION.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: problem with log in code

Post by beginner123 »

great thanks :)
Post Reply