2013-02-26 78 views
0

我是PHP新手,我有一個帶有表單的登錄頁面。我想驗證用戶,然後將用戶重定向到具有季節值的另一個頁面。當我提交頁面時不重定向,如果按F5頁面將彈出表單的重新提交消息。這是我的代碼。登錄表單無法在PHP中正確提交

<?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') ; ?> 
+0

第一錯誤報頭(「的login.php」 );它應該是header('Location:login.php'); ,一旦你發送標題沒有得到處理後 – youssDev 2013-02-26 19:38:56

+0

打開[**錯誤報告**](http://php.net/manual/en/function.error-reporting.php),然後你會意識到你的'print'語句和其他回顯html之前重定向導致'頭已經發送',之後重定向不會發生,除非你打開輸出緩衝。 – 2013-02-26 19:40:05

+0

以及如果您可以將所有方法外部化並將其包含在include_once中的最佳方法,因爲它很難理解帶有大量HTML內容的代碼 – youssDev 2013-02-26 19:41:44

回答

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'; 
} 
+0

我把退出代碼,它工作得很好。謝謝 – emurray 2013-02-26 20:40:37