mallen Posted September 23, 2007 Share Posted September 23, 2007 I have this simple sign in page. Sets a session for user and password. The problem is when I first load the page it shows the form and shows this error: Notice Undefined index: username in ........on line 21 Then I type in a wrong user and password to test it and it clears the error and shows the "You are not logged in". message like it should. The other problem is my logout page. Seems simple it says you have been logged out, but it sends you back to the login page, shows that I am logged in and still shows the error: Notice Undefined index: username in ........on line 21 Login Page: <?php session_start(); if ($dbc = @mysql_connect ('localhost', 'root', 'xxx')) { if (!mysql_select_db ('phpbb')) { die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); } } else { die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>'); } // convert username and password from _POST or _SESSION if($_POST){ $_SESSION['username']=$_POST["username"]; $_SESSION['password']=$_POST["user_password"]; } // query for a user/pass match $result=mysql_query("select * from phpbb_users where username='" . $_SESSION['username'] . "' and user_password='" . $_SESSION['password'] . "'"); // retrieve number of rows resulted $num=mysql_num_rows($result); // print login form and exit if failed. if($num < 1){ echo "You are not signed in.<br> <form method=POST action=login.php> username: <input type=text name=\"username\"> password: <input type=password name=\"user_password\"> <input type=submit> </form>"; } else { echo "You are signed in."; } exit; ?> --------------------------------------- Logout page: <?php session_start(); unset ($_SESSION['username']); unset ($_SESSION['password']); session_destroy(); echo "You have been successfully logged out. <br><br> You will now be returned to the login page. <META HTTP-EQUIV=\"refresh\" content=\"2; URL=login.php\"> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/ Share on other sites More sharing options...
rarebit Posted September 23, 2007 Share Posted September 23, 2007 Need some spaces in your mysql statement, I think... (not checked, but it's particular!) Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353606 Share on other sites More sharing options...
eldorik Posted September 23, 2007 Share Posted September 23, 2007 Is this login and logout connected to phpBB? If it is you can just use the phpBB login file. Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353619 Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 Notice Undefined index: turn off notices in php.ini or you'll see this all the time. Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353632 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 No I am using a phpbb database for testing. As for the spaces its working just not the sessions part. I don't want to turn off all errors. Is there one setting I should check? Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353634 Share on other sites More sharing options...
rarebit Posted September 23, 2007 Share Posted September 23, 2007 When you first load the login page you have no $_POST's set, so nothing is set when you use $_SESS in the query statement... Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353640 Share on other sites More sharing options...
teng84 Posted September 23, 2007 Share Posted September 23, 2007 Notice Undefined index: turn off notices in php.ini or you'll see this all the time. undefined index means u are calling an index of an array which is not set so if your gonna do something like print_r you cant see that index that you are trying to access eg. session print_r($_SESSION); Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353644 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 I turned off the notice. But the log out page will not end the session. It takes me back to the log in page where it shows me logged. 1. I need for the log out page to kill the session 2. I need a check to see if its set when the page loads Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353647 Share on other sites More sharing options...
teng84 Posted September 23, 2007 Share Posted September 23, 2007 when destroying a session no need for the unset Logout page: <?php session_start(); unset ($_SESSION['username']); unset ($_SESSION['password']); session_destroy(); simply session_start(); session_destroy(); to check if the session is set if (isset($_SESSION['username'])){ echo 'your logged'; } Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353657 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 I tried changing it to this and it still says i am logged in. <?php session_start(); session_destroy(); echo "You have been successfully logged out. <br><br> You will now be returned to the login page. <META HTTP-EQUIV=\"refresh\" content=\"2; URL=login.php\"> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353668 Share on other sites More sharing options...
teng84 Posted September 23, 2007 Share Posted September 23, 2007 put this on the top page right before your session start if (isset($_SESSION['username'])){ echo 'your logged'; } else { put here the code that will redirect } Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353674 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 That will send me back to my login page that says I am logged in. Nothing is killing the session to log me out. Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353684 Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 you may be seeing a cached page. change the headers on the login page to prevent caching, or, after logout send the user to the login page with a unique identifier appended, e.g., login.php?lo=12312312342 Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353686 Share on other sites More sharing options...
teng84 Posted September 23, 2007 Share Posted September 23, 2007 yep and can you explain how you kill your session ? Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353687 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 <?php session_start(); unset ($_SESSION['username']); unset ($_SESSION['password']); session_destroy(); or session_start(); session_destroy(); Both examples don't work. Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353696 Share on other sites More sharing options...
teng84 Posted September 23, 2007 Share Posted September 23, 2007 i mean how do you access your logout page? Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353699 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 At the bottom of my login page it displays a link if I am signed in. echo "You are signed in. <p><a href=logout.php>Click here to log out.</a></p> Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353702 Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 hm, i'd still check out the cached page problem. if you're just redirecting to login.php and login.php is a relatively simple HTML page, chances are the browser will cache that page with the last message. but i've been wrong before.... Quote Link to comment https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/#findComment-353704 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.