2016-11-26 148 views
0

當我進入頁面,我把認證代碼它會問我先登錄登錄之前。問題出在我登錄後不會進入想要去的頁面,然後再次點擊到同一頁面,它會要求我再次登錄。我在這裏做錯了什麼!?無法查看登錄後的頁面

loginform.php

<div class="container"> 
<div class="row"> 
    <di class="col-md-4 col-md-offset-4"> 
     <fieldset> 
      <legend>Login </legend> 
      <form method="post" action="loginProcess.php"> 
       <div class="form-group"> 
       <label>User name:</label> 
       <input type="text" name="username" class="form-control" required> 
       </div> 
       <div class="form-group"> 
       <label>Password:</label><input type="password" name="password" class="form-control" required> 
       </div> 
       <div class="form-group"> 
       <input type="submit" name="submit" value="Login" class="btn btn-default pull-right"> 
      </div> 
      </form> 
      </fieldset> 
    </di> 
</div> 

loginProcess.php

<?php 
session_start(); 
include ("dbCon.php"); 


    $username = filter_has_var(INPUT_POST, 'username') ? $_POST['username']: null; 
    $passWD = filter_has_var(INPUT_POST, 'password') ? $_POST['password']: null; 

     $sql = "SELECT passwordHash FROM te_users WHERE username = ?"; 
     $stmt = mysqli_prepare($conn, $sql);  

     mysqli_stmt_bind_param($stmt, "s", $username); 
     mysqli_stmt_execute($stmt); 
     mysqli_stmt_bind_result($stmt, $passWDHash); //Get the password hash from the query results for the given username and store it in the variable indicated 

     if(!empty($username)){ 
      if(!empty($passWD)){ 
       if (mysqli_stmt_fetch($stmt)) { //Check if a record was returned by the query. 
        if (password_verify($passWD,$passWDHash)){ 
         $username = $_SESSION['username']; 
         $login = $_SESSION['login']; 
         $_SESSION['login'] = true; 
         header("location:index.php"); 
        } 
        else 
        { 
         echo "<p>Sorry, we don't seem to have that password.</p>"; 
        } 
       } 
       else { 
        echo "<p>Sorry, we don't seem to have that username.</p>"; 
       } 
      } 
      else { 
       echo "<p>Please enter the password.</p>"; 
      } 
     } 
     else { 
      echo "<p>Please enter the username.</p>"; 
     } 


    mysqli_stmt_close($stmt); 
    mysqli_close($conn); 

?> 

otherpage.php

這是

if(empty($_SESSION['logged_in'])) 
{ 
    header('Location:login.php'); 
     exit; 
} 
else 
{ 

} 
+1

處處啓動會話 –

+1

任何腳本啓動會話,這裏有一個錯字'' ... –

+0

和另一個錯字' ' –

回答

1

您正在做的事情進行身份驗證使用的代碼在註冊成功登錄的部分中出現了錯誤的方法,並且還設置了與您在中檢查的名稱不同的$SESSION變量名,它們是否登錄了代碼

   if (password_verify($passWD,$passWDHash)){ 

        //$username = $_SESSION['username']; 
        //$login = $_SESSION['login']; 
        //$_SESSION['login'] = true; 

        $_SESSION['username'] = $username; 
        $_SESSION['logged_in'] = true; 

        header("location:index.php"); 
       } else { 
        echo "<p>Sorry, we don't seem to have that password.</p>"; 
       } 

還記得在希望使用會話

<?php 
session_start(); 

if(empty($_SESSION['logged_in'])) { 
    header('Location:login.php'); 
    exit; 
} else { 

} 
+0

它不起作用 –

+0

_它不工作_不是很有幫助。現在有什麼問題? – RiggsFolly

+0

@ Sith1234Sith您是否修復了''和''錯誤 – RiggsFolly