php - Why do i have to click twice?


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" "">


  $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 ?>.'); 

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

            header("Location: content1.php");

            header("Location: content2.php");

 //if the login form is submitted 

 if (isset($_POST['login'])) { 
  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());




<link rel="stylesheet" href="/css/butoane.css" type="text/css" />
<link rel="stylesheet" href="/css/admin_tools.css" type="text/css" />

<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" />

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






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

header('Location: content.php');

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

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


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.