neverett Posted January 29, 2008 Share Posted January 29, 2008 For some reason... when running the following code the $_SESSION variable never gets set. I always get "Your session is NOT working." login.php <?include("header.php");?> <div id="content2-pagetitle">Member Management</div> <div id="content-title-noshade-size2">Login</div> <br> <center> <table align="center"> <form name="login" action="checklogin.php" method="post"> <tr> <td>Email </td> <td><input type="text" name="email" size="25"></td> </tr><tr> <td>Password </td> <td><input type="password" name="password" size="25"></td> </tr> </table> <br> <input type="submit" name="Login" value="Login" title="submit"> </form> </center> <?include("footer.php");?> checklogin.php <?ob_start(); include("functions.php"); $email = $_POST['email']; $p = $_POST['password']; if($email != "" && $p != ""){ $pass = md5($p); // encrypt password $sql = "select * from users where email = '$email' and password = '$pass'"; $result = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($result); // count the number of rows that were queried if($count == 1){ $row = mysql_fetch_array($result); if($row['active'] == 1){ @session_start(); $_SESSION['user_id'] = $row['id']; $_SESSION['logged_in'] = true; sendredirect("http://www.ohiotrac.com/members/test.php"); }else if($row['active'] == 0){?> <center> <b>Your membership was not activated. Please open the confirmation email you received and click on the activation link. Thank you.</b> </center> <?} }else{?> <center> <b>Your login could not be authenticated. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} }else{?> <center> <b>Please use your email address and password to login. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} ob_end_flush();?> test.php <?@session_start(); include("header.php"); test(); include("footer.php");?> functions.php [this file contains the test() function] <?function test(){ if($_SESSION['logged_in'] == false){ echo "Your session is NOT working.<br><br>"; echo $_SESSION['logged_in']; }else if($_SESSION['logged_in'] == true){ echo "Your session is working.<br><br>"; echo $_SESSION['logged_in']; } }?> Link to comment https://forums.phpfreaks.com/topic/88311-login-system/ Share on other sites More sharing options...
cooldude832 Posted January 29, 2008 Share Posted January 29, 2008 do print_r($_SESSION) and see what it outputs Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-451917 Share on other sites More sharing options...
KrisNz Posted January 29, 2008 Share Posted January 29, 2008 take the @ off session_start(). See if you get any error messages. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-451918 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 take the @ off session_start(). See if you get any error messages. No errors. do print_r($_SESSION) and see what it outputs I get Array( ) Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-451979 Share on other sites More sharing options...
priti Posted January 29, 2008 Share Posted January 29, 2008 Hi, Referring you test script .I would suggest that in your test.php do $_SESSION['logged_in']=true; and then make a call to your test() function and see if the session is working or not. Regards Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-451981 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 Referring you test script .I would suggest that in your test.php do $_SESSION['logged_in']=true; and then make a call to your test() function and see if the session is working or not. The test() function returns true and displays "Your session is working." In other words, when I do that, it works. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452000 Share on other sites More sharing options...
priti Posted January 29, 2008 Share Posted January 29, 2008 So is that solves your problem or not.??? Actually in your code you have not set the $_SESSION['logged_in'] anywhere hence when you run that test script you find nothing no error and no output.what i told you is to set the $session['logged_in'] and check so in if() condition it finds the condition to be true and displays the message to you. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452080 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 I have set $_SESSION['logged_in'] to true in checklogin.php. It's there, I double checked. It somehow gets lost when I sendredirect() to the test.php page. I don't really like to send variables via the web address either (as I have considered doing that). I don't know what is going wrong. My redirect is a meta-refresh. Thanks for everything! Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452385 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 Any ideas??? I'm stumped. ??? Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452504 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 You need session_start() on the top of every page you require sessions. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452522 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 I noticed one place where I didn't have it, and I fixed it. Still not working though. Here is some updated code... test.php <?@session_start(); include("header.php"); test(); include("footer.php");?> functions.php <?function test(){ @session_start(); if($_SESSION['logged_in'] == false){ echo "Your session is NOT working.<br><br>"; echo $_SESSION['logged_in']; }else if($_SESSION['logged_in'] == true){ echo "Your session is working.<br><br>"; echo $_SESSION['logged_in']; } }?> login.php <?include("header.php");?> <div id="content2-pagetitle">Member Management</div> <div id="content-title-noshade-size2">Login</div> <br> <center> <table align="center"> <form name="login" action="checklogin.php" method="post"> <tr> <td>Email </td> <td><input type="text" name="email" size="25"></td> </tr><tr> <td>Password </td> <td><input type="password" name="password" size="25"></td> </tr> </table> <br> <input type="submit" name="Login" value="Login" title="submit"> </form> </center> <?include("footer.php");?> checklogin.php <?ob_start(); include("functions.php"); $email = $_POST['email']; $p = $_POST['password']; if($email != "" && $p != ""){ $pass = md5($p); // encrypt password $sql = "select * from users where email = '$email' and password = '$pass'"; $result = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($result); // count the number of rows that were queried if($count == 1){ $row = mysql_fetch_array($result); if($row['active'] == 1){ @session_start(); $_SESSION['user_id'] = $row['id']; $_SESSION['logged_in'] = true; sendredirect("http://www.ohiotrac.com/members/test.php"); }else if($row['active'] == 0){?> <center> <b>Your membership was not activated. Please open the confirmation email you received and click on the activation link. Thank you.</b> </center> <?} }else{?> <center> <b>Your login could not be authenticated. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} }else{?> <center> <b>Please use your email address and password to login. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} ob_end_flush();?> Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452546 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 checklogin doesn't have it at the top. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452551 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 But it shouldn't need it though because the $_SESSION variables are only being used in the one if statement right??? I'll try it though. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452557 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 To restate, session_start() needs to be the first line of code on every page you want to use sessions on. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452563 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 So I changed some things to better test my sessions and still not working. Here are the updated files (the login.php file remains the same as above)... test.php <?@session_start(); include("header.php"); if($_SESSION['logged_in'] == false){ echo "Your session is NOT working in test.php.<br><br>"; echo $_SESSION['logged_in']; }else if($_SESSION['logged_in'] == true){ echo "Your session is working in test.php.<br><br>"; echo $_SESSION['logged_in']; } test(); include("footer.php");?> functions.php <?// start sessions @session_start(); function test(){ if($_SESSION['logged_in'] == false){ echo "Your session is NOT working in functions.php.<br><br>"; echo $_SESSION['logged_in']; }else if($_SESSION['logged_in'] == true){ echo "Your session is working in functions.php.<br><br>"; echo $_SESSION['logged_in']; } }?> checklogin.php <?ob_start(); @session_start(); include("functions.php"); $email = $_POST['email']; $p = $_POST['password']; if($email != "" && $p != ""){ $pass = md5($p); // encrypt password $sql = "select * from users where email = '$email' and password = '$pass'"; $result = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($result); // count the number of rows that were queried if($count == 1){ $row = mysql_fetch_array($result); if($row['active'] == 1){ //@session_start(); $_SESSION['user_id'] = $row['id']; $_SESSION['logged_in'] = true; sendredirect("http://www.ohiotrac.com/members/test.php"); }else if($row['active'] == 0){?> <center> <b>Your membership was not activated. Please open the confirmation email you received and click on the activation link. Thank you.</b> </center> <?} }else{?> <center> <b>Your login could not be authenticated. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} }else{?> <center> <b>Please use your email address and password to login. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} ob_end_flush();?> Results Your session is NOT working in test.php. Your session is NOT working in functions.php. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452595 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 You don't seem to understand what first line means.... Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452605 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 Still not working. checklogin.php <?@session_start(); ob_start(); include("functions.php"); $email = $_POST['email']; $p = $_POST['password']; if($email != "" && $p != ""){ $pass = md5($p); // encrypt password $sql = "select * from users where email = '$email' and password = '$pass'"; $result = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($result); // count the number of rows that were queried if($count == 1){ $row = mysql_fetch_array($result); if($row['active'] == 1){ //@session_start(); $_SESSION['user_id'] = $row['id']; $_SESSION['logged_in'] = true; sendredirect("http://www.ohiotrac.com/members/test.php"); }else if($row['active'] == 0){?> <center> <b>Your membership was not activated. Please open the confirmation email you received and click on the activation link. Thank you.</b> </center> <?} }else{?> <center> <b>Your login could not be authenticated. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} }else{?> <center> <b>Please use your email address and password to login. <a href="http://www.ohiotrac.com/members/login.php">Click here to login.</a></b> </center> <?} ob_end_flush();?> Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452610 Share on other sites More sharing options...
neverett Posted January 29, 2008 Author Share Posted January 29, 2008 Anymore takers? I just don't understand why the variables get lost right after I send the redirect to test.php. Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-452754 Share on other sites More sharing options...
priti Posted January 30, 2008 Share Posted January 30, 2008 in checklogin.php print $_SESSION['logged_in'] after you do $_SESSION['logged_in'] and in test.php simply handle a case what is $_SESSION is empty. so like if(empty($_SESSION)) { echo 'session not yet assigned'; } try this Link to comment https://forums.phpfreaks.com/topic/88311-login-system/#findComment-453109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.