Trium918 Posted April 7, 2007 Share Posted April 7, 2007 I am able to login but everytime I log off I get the else statement saying that I never logged on. else { // if they weren't logged in but came to this page somehow echo "You were not logged in, and so have not been logged out.<br>"; } <? session_start(); $old_user = $valid_user; // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); ?> <h1>Log out</h1> <? if (!empty($old_user)) { if ($result) { // if they were logged in and are not logged out echo "Logged out.<br>"; } else { // they were logged in and could not be logged out echo "Could not log you out.<br>"; } } else { // if they weren't logged in but came to this page somehow echo "You were not logged in, and so have not been logged out.<br>"; } ?> <a href="session.php">Back to main page</a> Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/ Share on other sites More sharing options...
MadTechie Posted April 7, 2007 Share Posted April 7, 2007 would need to see the start of that script also put it in [ code] tags Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223250 Share on other sites More sharing options...
Trium918 Posted April 7, 2007 Author Share Posted April 7, 2007 This is the code that logs the user in <? session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Log in Page</title> </head> <body> <? if(!($_POST['username']) && !($_POST['password'])) { ?> <h1>Log in</h1> <form action="" method="post"> <table width="200" border="1" cellspacing="0" cellpadding="0"> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password" /></td> </tr> <tr> <td align="center" colspan="2"><input type="submit" value="Log in" /> </td> </tr> </table> </form> <? } else { // register_global = Off // inside the php.ini configuration file $username = stripslashes($_POST['username']); $password = stripslashes($_POST['password']); // Connect to MySql $db = mysql_connect("localhost") or die(mysql_error()); // Select the appropriate database mysql_select_db("member_auth") or die(mysql_error()); // query the database to see if there is a record which matched $query = "SELECT count(*) FROM user_info_auth WHERE username ='$username' AND password ='$password' "; $result = mysql_query($query); if (!$result) echo " Cannot run query!!"; if (mysql_num_rows($result) > 0) { //if they are in the database register the id $valid_user = $username; session_register("valid_user"); } if(session_is_registered("valid_user")){ echo "You are logged in as: $valid_user <br>"; echo "<a href=\"logout.php\">Log out</a><br>"; } else{ if(isset($username)){ //if they've tried and failed to log in echo " Could not log you in"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223259 Share on other sites More sharing options...
MadTechie Posted April 7, 2007 Share Posted April 7, 2007 2 scripts supplied don't even match!! Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223266 Share on other sites More sharing options...
Trium918 Posted April 7, 2007 Author Share Posted April 7, 2007 2 scripts supplied don't even match!! This session.php where they login <? session_start(); ?> Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Log in Page</title> </head> <body> <? if(!($_POST['username']) && !($_POST['password'])) { ?> Code: <h1>Log in</h1> <form action="" method="post"> <table width="200" border="1" cellspacing="0" cellpadding="0"> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password" /></td> </tr> <tr> <td align="center" colspan="2"><input type="submit" value="Log in" /> </td> </tr> </table> </form> <? } else { // register_global = Off // inside the php.ini configuration file $username = stripslashes($_POST['username']); $password = stripslashes($_POST['password']); // Connect to MySql $db = mysql_connect("localhost") or die(mysql_error()); // Select the appropriate database mysql_select_db("member_auth") or die(mysql_error()); // query the database to see if there is a record which matched $query = "SELECT count(*) FROM user_info_auth WHERE username ='$username' AND password ='$password' "; $result = mysql_query($query); if (!$result) echo " Cannot run query!!"; if (mysql_num_rows($result) > 0) { //if they are in the database register the id $valid_user = $username; session_register("valid_user"); } if(session_is_registered("valid_user")){ echo "You are logged in as: $valid_user <br>"; echo "<a href=\"logout.php\">Log out</a><br>"; } else{ if(isset($username)){ //if they've tried and failed to log in echo " Could not log you in"; } } } ?> This the logout.php <? session_start(); $old_user = $valid_user; // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); ?> <h1>Log out</h1> <? if (!empty($old_user)) { if ($result) { // if they were logged in and are not logged out echo "Logged out.<br>"; } else { // they were logged in and could not be logged out echo "Could not log you out.<br>"; } } else { // if they weren't logged in but came to this page somehow echo "You were not logged in, and so have not been logged out.<br>"; } ?> <a href="session.php">Back to main page</a> Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223270 Share on other sites More sharing options...
Trium918 Posted April 7, 2007 Author Share Posted April 7, 2007 I am able to log the user in but when I select to logout it states that I was never logged in. What would make this happen? Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223280 Share on other sites More sharing options...
MadTechie Posted April 7, 2007 Share Posted April 7, 2007 Erm... yeah by doing the following <? session_start(); $old_user = $valid_user; // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); ?> your removing referance that they were logged in, then your trying to check that referance Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223282 Share on other sites More sharing options...
MadTechie Posted April 7, 2007 Share Posted April 7, 2007 change //if they are in the database register the id $valid_user = $username; session_register("valid_user"); to //if they are in the database register the id $valid_user = $username; session["valid_user"]; and change <? session_start(); $old_user = $valid_user; // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); ?> to <? session_start(); $old_user = session["valid_user"]; // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); ?> Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223283 Share on other sites More sharing options...
Trium918 Posted April 7, 2007 Author Share Posted April 7, 2007 I re-register the variable $valid_user is that appropriate because I was getting syntex errors the other way. <? session_start(); $old_user = $valid_user; // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); ?> to <? session_start(); $old_user = session_register("valid_user"); // store to test if they *were* logged in $result = session_unregister("valid_user"); session_destroy(); Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223298 Share on other sites More sharing options...
Trium918 Posted April 7, 2007 Author Share Posted April 7, 2007 Man, I need all type of help. I just figure out that I am able to enter anything into the username text field. Could someone look over my code. Link to comment https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/#findComment-223311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.