w1ww Posted February 2, 2007 Share Posted February 2, 2007 Hello, This is a login script, everything works fine but when I login the session does not start so I get the message 'You're not in'.. Whats wrong? Here is the code for login and the test page: The login page: <? ob_start(); require_once($_SERVER['DOCUMENT_ROOT'].'/reg/db_connect.php'); if(isset($_SESSION['username']) && isset($_SESSION['password'])) { //REDIRECT TO USERS PROFILE... header("Location: test.php"); } //end if logged in //IF SUBMIT BUTTON PRESSED if(isset($_POST['submit'])) { if(!$_POST['username']) die("Error: You must enter your username before logging in."); if(!$_POST['password']) die("Error: You must enter your password before logging in."); //set cookie if checked if(!empty($_POST['stay_in'])) { $joined =''.$_POST['username'].'[]'.md5($_POST['password']).''; setcookie('login_cookie', $joined, 2147483647, '/', '.test.php'); } //end if //verify user... $get_user = mysql_query("SELECT * FROM `members` WHERE username = '".$_POST['username']."' AND user_password = '".md5($_POST['password'])."'"); $q = mysql_fetch_object($get_user); if(!$q) die("Login Failure: An error occured, please verify your username and password are correct."); //set session variables $_SESSION['logged_in'] = 1; $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; session_write_close(); header("Location: test.php"); } else { //show login form ?> <form name="login" method="post" action="<? $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td>Username:<input type="text" id="username" name="username"></td> </tr> <tr> <td>Password:<input type="password" id="password" name="password"></td> </tr> <tr> <td>Submit: <input type="submit" value="Submit" name="submit" id="submit"></td> </tr> <tr> <td>Remember? <input type="checkbox" name="stay_in[]" checked="yes"></td> </tr> </table> </form> <? }//end else ?> The test page with page headers: <? ob_start(); session_start( ); require_once($_SERVER['DOCUMENT_ROOT'].'/reg/db_connect.php'); //check cookie if ($_SESSION['logged_in'] != 1 && isset($_COOKIE['login_cookie'])) { list($user, $pass) = explode('[]', $_COOKIE['login_cookie']); $qu = mysql_query("SELECT `user_password` FROM `members` WHERE `username` = '".addslashes($user)."'"); if (mysql_num_rows($qu) == 1) { $passw = mysql_fetch_object($qu); if ($passw->user_password == $pass) { $_SESSION['logged_in'] = 1; $_SESSION['username'] = $user; $_SESSION['password'] = $pass; } } } if(!isset($_SESSION['username']) && !isset($_SESSION['password'])) { $_SESSION['logged_in'] = 0; $user = "Guest"; } // Lets see if we are in if ($_SESSION['logged_in'] == 1) { echo ("You're in"); } else { echo ("You're <b>NOT</b> in."); } ?> Can someone please check the code? Thanks for the help!! Link to comment https://forums.phpfreaks.com/topic/36809-solved-whats-wrong-here-sessions-cookies/ Share on other sites More sharing options...
pikemsu28 Posted February 2, 2007 Share Posted February 2, 2007 you need session_start() at the top of your login page Link to comment https://forums.phpfreaks.com/topic/36809-solved-whats-wrong-here-sessions-cookies/#findComment-175609 Share on other sites More sharing options...
w1ww Posted February 2, 2007 Author Share Posted February 2, 2007 Oh thank you You saved my day ! Someone please close the topic! Link to comment https://forums.phpfreaks.com/topic/36809-solved-whats-wrong-here-sessions-cookies/#findComment-175611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.