Darkmatter5 Posted June 30, 2008 Share Posted June 30, 2008 Here's my two files. login.php <?php include 'library/config.inc.php'; require('C:\wamp\www\library\opendb.php'); $pagedb = "members"; if(isset($_GET['try'])) { if(empty($_POST['username']) OR empty($_POST['password'])) { echo "Please fill in all the required fields!"; } else { $username = mysql_real_escape_string($_POST['username']); $password = md5($_POST['password']); $query = mysql_query("SELECT member_id FROM " .$dbname. "." .$pagedb. " WHERE mem_username = '" .$username. "' AND mem_password = '" .$password. "' ") or die(mysql_error()); list($user_id) = mysql_fetch_row($query); if(empty($user_id)) { echo "The username and password supplied, do not match any record!"; } else { $_SESSION['user_id'] = $user_id; header('location: userpanel.php'); // echo $user_id; } } } require('C:\wamp\www\library\closedb.php'); ?> <body> <form action="login.php?try=true" method="post"> Username: <input type="text" name="username"><br><br> Password: <input type="password" name="password"><br><br> <input type="submit" value="Login!"> </form> </body> </html> userpanel.php <?php require('C:\wamp\www\library\opendb.php'); include 'library/config.inc.php'; $pagedb = "members"; /* session_start(); if(isset($_SESSION['user_id'])) { $query = mysql_query("SELECT mem_username FROM " .$dbname. "." .$pagedb. " WHERE member_id = " .$_SESSION['user_id']. " LIMIT 1") or die(mysql_error()); list($username) = mysql_fetch_row($query); echo "Hi " .$username. ", welcome to your profile!"; } else { echo "Please login before opening the user panel!"; }*/ echo "SESSION = " .$_SESSION['user_id']; /* echo "SELECT mem_username FROM " .$dbname. "." .$pagedb. " WHERE member_id = " .$_SESSION['user_id']. " LIMIT 1";*/ require('C:\wamp\www\library\closedb.php'); ?> When I type in valid user credentials and click the "Login!" button it'll forward me to the userpanel.php page, but it doesn't seem to carry over the session. How can I properly check if the session is working? As you can see in the login.php file I have commented out the line "echo $user_id". If I uncomment that line and comment out the two lines before it, it will properly display the user_id queried from mysql, so I know the query works, I just don't know if the session code is working. Please help! Link to comment https://forums.phpfreaks.com/topic/112592-help-with-sessions/ Share on other sites More sharing options...
keeB Posted June 30, 2008 Share Posted June 30, 2008 Your session_start(); is commented out. Link to comment https://forums.phpfreaks.com/topic/112592-help-with-sessions/#findComment-578237 Share on other sites More sharing options...
revraz Posted June 30, 2008 Share Posted June 30, 2008 I don't even see a session_start() in login.php It should be the first line of code on both pages. Link to comment https://forums.phpfreaks.com/topic/112592-help-with-sessions/#findComment-578239 Share on other sites More sharing options...
Darkmatter5 Posted June 30, 2008 Author Share Posted June 30, 2008 Thanks for your reply! I just noticed that. I fixed that part to this. <?php require('C:\wamp\www\library\opendb.php'); include 'library/config.inc.php'; $pagedb = "members"; session_start(); if(isset($_SESSION['user_id'])) { echo "SESSION = " .$_SESSION['user_id']; /* $query = mysql_query("SELECT mem_username FROM " .$dbname. "." .$pagedb. " WHERE member_id = " .$_SESSION['user_id']. " LIMIT 1") or die(mysql_error()); list($username) = mysql_fetch_row($query); echo "Hi " .$username. ", welcome to your profile!";*/ } else { echo "Please login before opening the user panel!"; } /* echo "SELECT mem_username FROM " .$dbname. "." .$pagedb. " WHERE member_id = " .$_SESSION['user_id']. " LIMIT 1";*/ require('C:\wamp\www\library\closedb.php'); ?> But it still doesn't work and now says "Please login before opening the user panel!" as I wrote in the userpanel.php file in the else to the if(isset($_SESSION['user_id'])) code. Anymore help? Link to comment https://forums.phpfreaks.com/topic/112592-help-with-sessions/#findComment-578242 Share on other sites More sharing options...
Darkmatter5 Posted June 30, 2008 Author Share Posted June 30, 2008 Forgive me for my ignorance, this is my first Session attempt. revraz thanks! it's working now! thank you everyone for your help! Link to comment https://forums.phpfreaks.com/topic/112592-help-with-sessions/#findComment-578244 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.