steve1388 Posted March 31, 2011 Share Posted March 31, 2011 This is my first post here. G'day everyone. I'm having trouble with getting a session recognized from one page to another. I did a search and used the advice but I'm still having trouble. I have a page called checklogin.php that processes the information submitted for a member to login, and redirect it to login_success.php if the login was successful. The problem I have is when I test the session in the second page it tells me there is no session. Here's my code (I omitted the db connection code): <?php // username and password sent from form $myusername=$_POST['user']; $mypassword=$_POST['pass']; ?> <? // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername']=$myusername; $_SESSION['mypassword']=$mypassword; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> after I am redirected to login_success.php: <?php session_start(); $_SESSION['myusername']; if (isset($_SESSION['myusername'])) { $loggedin = TRUE; return $loggedin; echo "logged in."; } else echo "not logged in"; ?> It echoes "not logged in". I've struggled with this for days and I don't know what's wrong. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/ Share on other sites More sharing options...
Skewled Posted March 31, 2011 Share Posted March 31, 2011 if (isset($_SESSION['myusername'])){ echo "logged in."; } else { echo "not logged in"; } That's all you should need. The other parts weren't needed. Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1194804 Share on other sites More sharing options...
kenrbnsn Posted March 31, 2011 Share Posted March 31, 2011 You need to put <?php session_start(); ?> in every script where you use sessions. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1194816 Share on other sites More sharing options...
steve1388 Posted March 31, 2011 Author Share Posted March 31, 2011 awesome. it works now. I'd like to print the username something like "Hi, [user] you are logged in". How can I call the username variable in every other page? I'm new at this, in case you can't tell Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1194826 Share on other sites More sharing options...
Skewled Posted March 31, 2011 Share Posted March 31, 2011 You call it from $_SESSION['myusername'] you can use it directly or assign it to a variable Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1194830 Share on other sites More sharing options...
steve1388 Posted March 31, 2011 Author Share Posted March 31, 2011 When I try to echo it just says "Welcome" with no username. Tried assigning it to a variable, but no go, same thing. if (isset($_SESSION)) { echo "Welcome " . $_SESSION['myusername']; } else echo "not logged in"; Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1195073 Share on other sites More sharing options...
kenrbnsn Posted March 31, 2011 Share Posted March 31, 2011 Do <?php if (isset($_SESSION)) { echo '<pre>' . print_r($_SESSION,true) . '</pre>'; } ?> This will show you what's in the session array. Post it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1195077 Share on other sites More sharing options...
steve1388 Posted March 31, 2011 Author Share Posted March 31, 2011 here it is Array ( [myusername] => ) Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1195082 Share on other sites More sharing options...
kenrbnsn Posted March 31, 2011 Share Posted March 31, 2011 That is telling us that the session variable $_SESSION['myusername'] was created with a blank value. You have to go back to the script that assigns that value to see where the problem is. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1195102 Share on other sites More sharing options...
j9sjam3 Posted March 31, 2011 Share Posted March 31, 2011 Login.php <?php session_start(); if(isset($_POST['submit'])) { // check username and password from db or whatever if($sucessfulLogin) { $_SESSION['username'] = $username; header("Location: index.php"); } else { echo "Incorrect login"; } } ?> Index.php <?php if(!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } ?> <html> <head>... Welcome back, <?php echo $_SESSION['username']; ?>. Hope your having a good day! Quote Link to comment https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1195112 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.