Jump to content

problem with logout form


Ashish Sood

Recommended Posts

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

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 by Ashish Sood
Link to comment
Share on other sites

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

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

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

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 by Ashish Sood
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...