Unknown98 Posted November 3, 2011 Share Posted November 3, 2011 I'm working on a small project that requires a user login function. There's only going to be about 10 members in the database. I've followed a tutorial of how to make a login system. I've never really worked with sessions before, but I did some research and get the general concept of them. What I want to do is store the user's username in a session when they log in, so I can run queries and such later specific to that user. How would I do that? I was able to manually set a session, but I need to set the session equal to what their username is on login. I've tried doing that below, but I don't think it's working. Either that, or I'm not calling the session correctly in the other page where I want to display their username. Here's the code for the page that checks if the login is correct: // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $encrypted_mypassword=md5($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'"; $result=mysql_query($sql); // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); // 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['password'] = $encrypted_mypassword; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Thanks in advance for any replies Quote Link to comment https://forums.phpfreaks.com/topic/250366-session-values/ Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 You need to call session_start on any page that uses the $_SESSION array. Quote Link to comment https://forums.phpfreaks.com/topic/250366-session-values/#findComment-1284597 Share on other sites More sharing options...
Unknown98 Posted November 3, 2011 Author Share Posted November 3, 2011 That was the problem. I wasn't calling the session_start() on the check login page Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/250366-session-values/#findComment-1284770 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.