php - Why do i have to click twice?

258

I have a login page where i have to : insert user and password (1 time); click the button(the login button) twice to actually login.I use Xamp with PHP 5.3 and HeidiSQL .If i'm already in session, it skips the login window and redirects to content page else the form is submitted(which is what i want it to do).This is my code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
 session_start();
 include("../conect.php");



 if(isset($_SESSION['name']))

 { 
  $username = $_SESSION['name']; 
  $pass = $_SESSION['pass'];
  $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

  while($info = mysql_fetch_array( $check )) 
    {
        if ($pass != $info[2]){
          header('.<?php $_PHP_SELF ?>.'); 
        }

        else
          {  if($_SESSION['role']==0) 
            header("Location: content0.php");

            if($_SESSION['role']==1) 
            header("Location: content1.php");

            if($_SESSION['role']==2) 
            header("Location: content2.php");
          }
    }
 }


 //if the login form is submitted 

 if (isset($_POST['login'])) { 
  $nume=mysql_real_escape_string($_POST['username']);
  $parola=md5(mysql_real_escape_string($_POST['pass']));
  if((!$_POST['username']) || (!$_POST['pass'])) {

    die('You did not fill in a required field.');

  }

  $check_pass = mysql_query("SELECT * FROM users WHERE username = '$nume'")or die(mysql_error());
  $check2 = mysql_num_rows($check_pass);

  if ($check2 == 0) {
    die("That user does not exist in our database");
  }

  while($data = mysql_fetch_array( $check_pass )) {
      //gives error if the password is wrong
      if ($parola != $data[2]) {

        die('Incorrect password, please try again.');
      }
      else { 
            $result = mysql_query("SELECT * FROM users WHERE username = '$nume'")or die(mysql_error());
            while($data=mysql_fetch_row($result)){
              $_SESSION['name']=$data[1];
              $_SESSION['pass']=$data[2];
              $_SESSION['role']=$data[3];
            }
          }
        } 

} 


mysql_close($con);
?>

<html>

<head>
<title>Login</title>
<link rel="stylesheet" href="/css/butoane.css" type="text/css" />
<link rel="stylesheet" href="/css/admin_tools.css" type="text/css" />
<script>

</script>
</head>
<body id="login_background">

<div id="ambele">
<div >

  <form action="<?php $_PHP_SELF ?>" method="post">
  <label for="username">Username</label><input type="text" id="username" name="username" maxlength="40"/>
  <label for="password">Password</label><input type="password" id="password"name="pass" maxlength="20"/>
  <input type="submit" name ="login" value="Login" />
  </form>
</div>

<div id="register"> 
<a href="registration_form.php" id="reg">Not registered yet? Go to Registration</a>
</div>

</div>

</body>

</html>
832

Answer

Solution:

I think you need to exit your script to do redirection.

<?php
header('Location: content.php');
die();
?>

Also, you put the PHP tag in the wrong position.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
[...]
?>
[...]

This will cause theHeaders already sent error in PHP because you already send something to the client before you do redirection.

People are also looking for solutions to the problem: PHP, Python Django File Download Control

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.