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
https://forums.phpfreaks.com/topic/256072-problem-with-_session-not-working/
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! 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.