0

我是 PHP 新手,我有一个带有表单的登录页面。我想对用户进行身份验证,然后将用户重定向到具有季节值的另一个页面。当我提交页面时没有重定向,如果按 F5,页面将弹出表单的重新提交消息。这是我的代码。

<?php
        session_start();
         include('../includes/header.php');
           $title="Login";

?> 
<?php  

        include('../includes/authenticate.php');
        include('../includes/dbschema.php');


        //if the user has not logged in
        if(!isLoggedIn())
        {

           if($_SERVER['REQUEST_METHOD']== 'POST'){

        //include('../includes/authenticate.php');

        $username = $_POST['username'];
        $password = $_POST['password'];

        //sanitize username
    $username = mysql_real_escape_string($username);

        if(isset($_POST['username']) && isset($_POST['password'])){


        $query = "SELECT password, salt FROM user WHERE username = '$username';";

        $result = mysql_query($query);
        if(mysql_num_rows($result) < 1) //no such user exists
        {
            header('login.php');
           $errmessage = "Invalid user";
           print "<p id\"errmessage\"> $errmessage </p>";

        }

        $userData = mysql_fetch_array($result, MYSQL_ASSOC);
        $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
        if($hash != $userData['password']) //incorrect password
        {
            header('login.php');


        }
        else
        {


            validateUser(); //sets the session data for this user

        }
        //redirect to another page or display "login success" message
         header('Location: actividades.php');

        }


        }
        else
        {
          $status = "logout";
           print  "<p id=\"usernamelink\">Bienvenido,<a id=\"usernamecolor\"> " . $_SESSION['username'] . " </a></p>";
          print  "<a id=\"logoutlink\" href=\"../includes/logout.php   \">Log out</a>";
          //page content follows
        }

          ?>

        </br>

          <div id="logindiv">
                  <?php   print "<h1 id=\"logintitle\">Login</h1>";?>
                       <form id="loginform" name="login" action="login.php" method="post" >
            Username: <input id="inplogin" type="text" name="username" />
                           <br/><br/>
            Password: <input id="inplogin" type="password" name="password" />
                    <br/>   


            <input id="btnlogin" type="submit" value="Login" />
        </form>
              </div>

<?php include('../includes/footer.php') ; ?>
4

1 回答 1

0

你应该exit;在重定向之后。并将错误传递给您的登录脚本,例如:

if(login_fails()){
  header('Location: login.php?errorCode=1');
  exit;
}

在您的login.php脚本中,检查是否$_GET['errorCode']存在并显示错误消息:

$errors = array(
  1 => 'Incorrect password',
);

if(isset($_GET['errorCode'])){
  $code = $_GET['errorCode'];
  print isset($errors[$code]) ? $errors[$code] : 'Unknown error';
}
于 2013-02-26T19:49:40.963 回答