cheesybiscuits Posted January 30, 2012 Share Posted January 30, 2012 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 More sharing options...
Pikachu2000 Posted January 30, 2012 Share Posted January 30, 2012 Do you have session_start(); at the head of the members.php script, before any output is sent to the browser? Link to comment https://forums.phpfreaks.com/topic/256072-problem-with-_session-not-working/#findComment-1312751 Share on other sites More sharing options...
cheesybiscuits Posted January 30, 2012 Author Share Posted January 30, 2012 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 https://forums.phpfreaks.com/topic/256072-problem-with-_session-not-working/#findComment-1312752 Share on other sites More sharing options...
jotorres1 Posted January 30, 2012 Share Posted January 30, 2012 Try putting session_start() on line 1 Link to comment https://forums.phpfreaks.com/topic/256072-problem-with-_session-not-working/#findComment-1312755 Share on other sites More sharing options...
cheesybiscuits Posted January 30, 2012 Author Share Posted January 30, 2012 Thanks guys worked like a charm. Link to comment https://forums.phpfreaks.com/topic/256072-problem-with-_session-not-working/#findComment-1312757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.