2014-09-30 55 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