Jump to content

Problem with $_SESSION not working...


cheesybiscuits

Recommended Posts

Hi,

I'm kinda new to php, so I followed a video tutorial on making a login/registration system and nearly everything works, except for when a registered user follows a link to the members.php page, the $_SESSION variable is supposed to check if they are logged-in and say "Welcome, [name of user]!. But it keeps saying I need to be logged in, even though I am.

 

The php code from the members.php page.

if ($_SESSION['username']) {
     echo "Welcome," .$_SESSION['username']. "!";
} else {
     die ("You must be logged in.");
}

 

This is the php code form the login page.

session_start();
$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password) {
	$connect = mysql_connect("localhost","root","password") or die ("Unable to connect");
	mysql_select_db("database") or die ("Unable to find database");

	$query = mysql_query("SELECT * FROM users WHERE username='$username'");
	$numrow = mysql_num_rows($query); 

	if ($numrow!=0) { //
		while ($row = mysql_fetch_assoc($query)) {
			$dbusername = $row['username'];
			$dbpassword = $row['password'];
		}
		if ($username==$dbusername&&md5($password)==$dbpassword)
			echo "You're in! - <a href=\"members.php\">Go to members page</a>";
			$_SESSION['username'] == $dbusername;
		} else {
			echo "Incorrect password";
		}
	} else {
		die ("That user doesn't exist");
	}	
} else {
	die("Please enter a username and password");
}

Any help would be much appreciated. 

 

 

Link to comment
Share on other sites

I tried it, and it now sends the variable, but I get this message

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /srv/disk1/956153/www/lincscrusade.mywebcommunity.org/members.php:6) in /srv/disk1/956153/www/lincscrusade.mywebcommunity.org/members.php on line 7
Welcome,cheesybiscuits! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.