2014-09-30 56 views
0

我的網站有問題,問題出在這個: 當我嘗試登錄並輸入正確的登錄信息時,我不斷收到的process_login頁和頭不想要我重定向回索引頁面......這只是發生在我的網頁,在本地主機上一切都很完美的罰款.. 代碼:一次又一次地在進程頁面上填充(空白頁面)

登錄模式

<div class="modal fade" id="login" role="dialog"> 
       <div class="modal-dialog"> 
        <div class="modal-content"> 
         <center><div class="modal-header"> 
          <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
          <div class="alert alert-info">Login page</div> 
         </div></center> 
         <div class="modal-body"> 
          <form role="form" action="process_login.php" method="post"> 
          <div class="form-group"> 
           <label for="exampleInputUsername">Username</label> 
           <input type="text" name="username" class="form-control" id="username_login" placeholder="Enter username"><span id="span_username_login"></span> 
          </div> 
          <div class="form-group"> 
           <label for="exampleInputPassword1">Password</label> 
           <input type="password" name="password" class="form-control" id="password_login" placeholder="Password"><span id="span_password_login"></span><br/> 
          </div> 
           <a href="#recover" data-toggle="modal" ><p>I've forgotten my password</a></p> 
          <div class="modal-footer"> 
          <center><input type="submit" name="submit" class="btn btn-danger" value="Sign in"/></center> 
         </div> 
          </form> 
        </div> 
       </div> 
      </div> 
      </div> 

的process_login .php頁面

<?php 
require_once 'includes/initialize.php'; 
require_once 'classes/database.php'; 
require_once 'classes/bcrypt.php'; 
require_once 'classes/user.php'; 


    if(isset($_POST['submit'])) 
    { 

      $username = filter_input(INPUT_POST, 'username' , FILTER_SANITIZE_STRING); 
      $password = filter_input(INPUT_POST, 'password' , FILTER_SANITIZE_STRING); 

      //if input fields ain't filled , redirecting back to index page 
      if(empty($username) || empty($password))   
      {              
       $_SESSION['emptyfields'] = ''; 
       header("Location: index.php"); 

      } 
      //if that particular username doesnt exist in database , redirecting back     to index page 
      if($userclass->userExists($username) == false) 
      { 
       $_SESSION['usernamedoesntexist'] = ''; 
       header("Location: index.php"); 
      } 
      //if that particular email isnt confirmed yet , redirecting back to index page 
      if($userclass->emailConfirmed($username) == false) 
      { 
       $_SESSION['emailnotconfirmed'] = ''; 
       header("Location: index.php"); 
      } 
      if($userclass->banned($username) == false) 
      { 
       //if that particular username is banned , redirecting back to index page 
       $_SESSION['banned'] = '';  
       header("Location: index.php"); 

      }  


      $login = $userclass->login($username, $password); 
      if($login == false) 
      { 
       $_SESSION['errorwithlogin'] = '';  //incorect username - password combo 
       header("Location: index.php"); 
      } 
      else 
      { 
       $_SESSION['user_auth'] = TRUE;   //corect username - password combo 
       $_SESSION['user_id'] = $login; 

       $_SESSION['loginsuccessfull'] = ''; 
       header("Location: index.php"); 

      } 



    } 
     else 
     { 
      $_SESSION['errorwithprocessing'] = ''; 
      header("Location: index.php"); 
     }  

?> 
<?php 
unset($_SESSION['errorwithprocessing']); 
unset($_SESSION['loginsuccessfull']); 
unset($_SESSION['errorwithlogin']); 
unset($_SESSION['banned']); 
unset($_SESSION['emailnotconfirmed']); 
unset($_SESSION['usernamedoesntexist']); 
unset($_SESSION['emptyfields']); 
?> 

類user.php的頁面(即在與記錄連接行)

public function login($username, $password) 
    { 
     global $db; 
     global $bcrypt; 

     $stm = $db->connection->prepare("SELECT `user_id`,`password` FROM `users` WHERE `username` = ?");  

     $stm->bindValue(1, $username); 

     try 
     {   
        $stm->execute(); 
        $data = $stm->fetch();  
        $stored_password = $data['password'];  
        $id = (int) $data['user_id']; 

        if($bcrypt->verify($password, $stored_password) === true)   
        { 
         return $id; 
        } 
        else 
        { 
         return false; 
        } 
     } 
     catch(PDOException $e) 
     { 
      die($e->getMessage()); 
     } 

    } 

歡迎任何幫助,在此先感謝..

+1

如果沒有進一步的代碼應該執行,不要忘了在'header'之後加'exit;'。並啓用錯誤消息的輸出。 – Cheery 2014-09-30 19:16:53

+0

那麼,你應該首先在重定向後添加'exit;'(例如'header('Location:test.php'); exit;')。 PHP將繼續閱讀代碼,因此您必須在重定向時停止它。 – 2014-09-30 19:17:22

+0

process_login.php腳本中有大量額外的代碼。無論發生什麼事情,你都希望將用戶發送回index.php,所以你最好只在最後的else語句後添加一次 – ksealey 2014-09-30 19:27:34

回答

0

從我能看到我假設你開始在你進入重定向代碼之前的一個會話。也許在你的一個包含文件中?無論如何,在致電session_start()之前,您都需要致電所有header()功能。根據我的經驗,不這樣做會導致不可預知的結果,這取決於我運行的PHP版本和服務器。

但是,我明白,這並不總是一種選擇,考慮到這一點,我使用以下代碼作爲解決方案header("Location:index.php");這將使用JavaScript重定向。

echo '<script type="text/javascript">window.location="index.php";</script>'; 
+0

session_start()在我的初始化文件中,但nvm mate,1000謝謝你。 ..這對我來說工作得很好,現在一切都好了...再次感謝.. – pfktiten 2014-09-30 19:39:58

+0

真棒,很高興聽到 – ksealey 2014-09-30 19:46:41