Ashish Sood Posted December 5, 2012 Share Posted December 5, 2012 HI ,I am facing a problem with my logout form after destroying a session my page is not redirected to login.php, even after refresh the same page logout.php page i am gettting Notice: Undefined index: username in /var/www/html/checklist/project/login/logout.php on line 4 username, That mean my session is destroyed, but dont know why i will not get redirected..please guys advice me what i am doing wrong in this , Please find my code below. <?phpsession_start();if(isset($_SESSION['username'])){ session_destroy(); header("location: login.php"); }?> Link to comment Share on other sites More sharing options...
justsomeguy Posted December 5, 2012 Share Posted December 5, 2012 Capitalize Location. It might also make sense to move the redirect out of the if statement, if the page is just there to log out a user then might as well always redirect unless you want to show some message on the page. Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 6, 2012 Author Share Posted December 6, 2012 Thanks More Human Than Human, Its working for me now. :Happy: Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 6, 2012 Author Share Posted December 6, 2012 (edited) I want to know one more thing, after successfully login my page will be redirect to index.php, But if someone open again login.php he also should be redirect to index.php. please guys tell me what condition i need to put in login.php page. I tried below, but after again open login.php, it am getting the error. <?phpif(isset($_SESSION['username'])) { header('Location: index.php'); } else { header('Location: login.php'); }?> ERROR The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies. Edited December 6, 2012 by Ashish Sood Link to comment Share on other sites More sharing options...
justsomeguy Posted December 6, 2012 Share Posted December 6, 2012 Remove the else. You're telling it to keep redirecting to itself if you're not logged in, which is why the browser says the redirection will never stop. If they're not logged in you don't want to redirect, you want to show the login form. Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 7, 2012 Author Share Posted December 7, 2012 Thanks for the reply Sir,I will show my complete code, i created two files,1.login.php (Simple HTML file In this file i included validate_login.php file at starting)2 validate_login.php (This is the main file where is check the codition and handling sessions) Login.php <?php include ('validate_login.php'); ?><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>User Login --User Login Form! </title></head><body><div><center><h3><font face="Lucida Sans Unicode, Lucida Grande, sans-serif">Login <br /> ACCESS </font></h3><br /></center></div><br /><br /><br /><center><div id="form"><form action="login.php" method="post"><fieldset><legend> USER LOGIN</legend><p><div><label for="username">*USERNAME:</label><input type="text" name="username" /><br /><p></div><p><div><label for="username">*PASSWORD:</label><input type="password" name="password" /></div></p><br /><br /> <input type="submit" name="submit" value="Login" /> <input type="reset" name="reset" value="Reset" /></div></p></fieldset></form></center></body></html> Validate_login.php <?phpsession_start();if(isset($_SESSION['username'])) //if session is already there than redirect to index.php page { header('Location: index.php'); }?><?php if(isset($_POST['submit'])) { include('connection.php'); $username= $_POST['username']; $password= $_POST['password']; if(empty($username)) { echo "<b><font color='red'> ERROR: <br> "; echo "Username field can't be empty </font></b>"; } if(empty($password)) { echo "<br><b><font color='red'> ERROR: <br> "; echo "Password field can't be empty </font></b>"; } else { $password = md5($password); $sql = mysql_query("SELECT * FROM login WHERE username='$username' AND password='$password' LIMIT 1 ") or die ("Login Error " .mysql_error()); $num_rows = mysql_num_rows($sql); //echo "$num_rows"; if ($num_rows == 1) { $fetch=mysql_fetch_array($sql); extract($fetch); //echo "Current Logging User Is $username"; $_SESSION['username'] = $username; if(isset($_SESSION['username'])) { header('Location: index.php'); } } else { echo "<font color='red'>"; echo "<b>Invalid Credentials"; echo "</font></b>"; } }}?> I am successfully redirect to index.php page, after removing else part, but on the login.php i am getting a session error. Notice: Undefined variable: _SESSION in /var/www/html/checklist/project/login/login.php on line 3 Link to comment Share on other sites More sharing options...
justsomeguy Posted December 7, 2012 Share Posted December 7, 2012 It says you're trying to use $_SESSION on line 3 of login.php. I don't see that code there. Even so, remove the extra line breaks at the top of that file. Link to comment Share on other sites More sharing options...
westman Posted December 7, 2012 Share Posted December 7, 2012 yes have no line brakes meaning put <?php at the top of the page (line 1) also i had a problem with $_SESSION before when i was changing my host provider please check 3 thinks and match them 1. version of php on your server 2. version of php you are writing 3. version of $_SESSION working today (look on php.net) Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 8, 2012 Author Share Posted December 8, 2012 (edited) It says you're trying to use $_SESSION on line 3 of login.php. I don't see that code there. Even so, remove the extra line breaks at the top of that file. This line coming from validate_login.php page i think not from login.php page, becuase i included this page at the top of login.php. Edited December 8, 2012 by Ashish Sood Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 8, 2012 Author Share Posted December 8, 2012 I sorted out my problem, i closely checked my login.php page and found and error and i remove tha line from thereerror line is echo $_SESSION['username']; I removed the above line now every thing is running fine Thanks alot guys for your efforts 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now