graham23s Posted July 2, 2007 Share Posted July 2, 2007 Hi Guys, i normally use cokies but have been playing around with sessions, i have this basic script: login.php <form action="check_login.php" method="POST" /> Username:<input type="text" name="username" /> Password:<input type="password" name="password" /> <input type="submit" value="Login" /> </form> check_login.php <?php session_start(); mysql_connect("localhost", "root", "xxxxxxxx"); mysql_select_db("xxxxxxxxxxxx"); $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($sql); if (mysql_num_rows($result) == 0) { echo "Username and password combination not found."; } else { $row = mysql_fetch_array($result); $_SESSION['username'] = $username; header("members.php"); } ?> members.php <?php session_start(); if (!isset($_SESSION['username'])) { //Not logged in, redirect to login page header("login.php"); } //If they pass that, they're logged in, and are authorized to view whatever else is in this file echo 'YOU ARE LOGGED IN'; ?> i was wanting to make the users , username the logged in variable so i could later on use it to drag info from mysql about them but it doesn't seem to get past check_login.php what am i doing wrong? thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/ Share on other sites More sharing options...
john010117 Posted July 2, 2007 Share Posted July 2, 2007 You didn't start $_SESSION['username'] in check_login.php... Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/#findComment-288388 Share on other sites More sharing options...
graham23s Posted July 2, 2007 Author Share Posted July 2, 2007 Hi John, sorry do you mean like this: <?php session_start(); $_SESSION['username']; mysql_connect("localhost", "root", "milkybar"); mysql_select_db("myliveaudition"); $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($sql); if (mysql_num_rows($result) == 0) { echo "Username and password combination not found."; } else { $row = mysql_fetch_array($result); $_SESSION['username'] = $username; header("members.php"); } ?> cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/#findComment-288391 Share on other sites More sharing options...
john010117 Posted July 2, 2007 Share Posted July 2, 2007 Nevermind. You started the session already ($_SESSION['username'] = $username). I overlooked that before. Sorry. Solution: check_login.php: <?php session_start(); mysql_connect("localhost", "root", "xxxxxxxx"); mysql_select_db("xxxxxxxxxxxx"); $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($sql); if (mysql_num_rows($result) == 0) { echo "Username and password combination not found."; } else { while($row = mysql_fetch_array($result)) { $_SESSION['username'] = $row['username']; } header("members.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/#findComment-288393 Share on other sites More sharing options...
graham23s Posted July 2, 2007 Author Share Posted July 2, 2007 Hi John, thanks for that, it seems to stick at check_login.php. it doesn't goto members.php (even though everything looks good it must have found the correct credentials or it would have said otherwise) is there something i have overlooked? cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/#findComment-288398 Share on other sites More sharing options...
teng84 Posted July 2, 2007 Share Posted July 2, 2007 should be header("location:members.php"); Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/#findComment-288401 Share on other sites More sharing options...
graham23s Posted July 2, 2007 Author Share Posted July 2, 2007 Thanks guys that solved it:) Graham Quote Link to comment https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/#findComment-288405 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.