2017-02-25 132 views
-1

我在登錄頁面遇到問題。當我按下登錄按鈕時,只有在記住我複選框被選中的情況下,我才能進入用戶面板。如果它沒有被檢查,那麼它保持登錄頁面。我無法弄清楚錯誤。登錄頁面錯誤

include("Database/database.php"); 
session_start(); 
if(isset($_COOKIE['username']) and isset($_COOKIE['passcode'])) { 
    $username = $_COOKIE['username']; 
    $passcode = $_COOKIE['passcode']; 

    $_SESSION['name'] = $username; 
    $_SESSION['pwd'] = $passcode; 

    header("Location: useraccount.php"); /*Checks if cookies are set then takes me directly to useraccount.php */ 

    } else if (isset($_POST['login'])) { 
     $username = $_POST['username']; 
     $userpwd = $_POST['userpwd']; 
     $rem = $_POST['remember']; 

     $query = "SELECT * FROM login WHERE userName = '$username' AND Password = '$userpwd'"; 

     $result = mysqli_query($link, $query); 
     $row = mysqli_num_rows($result); 

     if ($row > 0) { 
      $_SESSION['name'] = $username; 
      $_SESSION['pwd'] = $userpwd; 

      if (isset($_POST['remember']))  /*if remember me checkbox is checked than it set cookies and it takes me to useraccount.php page. But if I donot check the check box (remember me) than I am where I am on login page. */ 
      { 
       setcookie("username", $username, time()+60*60*7); 
       setcookie("passcode", $userpwd, time()+60*60*7); 
      } 

      header("Location: useraccount.php");  /* It should procees to    useracocunt.php page but it stays on login.php*/ 
     } else { 
      header("Location: login.php");  /*Used this for testing purpose if login fails it takes me to homepage which is working fine*/ 
     } 
     mysqli_close($link); 

    } 
} 

回答

1

我找到了解決這個問題的辦法。這是我所有的錯誤,因爲如果我沒有設置cookie,我會將標題重定向到登錄頁面。

  • 所以,如果你正面臨到頭檢查,如果你是它的標題放在適當的位置該文件「URL」或
  • 必須有一些拼寫錯誤相關的問題。
0

如果語句對臉頰保存到session中,請添加會話,但無處檢查。

<?php 
     include("Database/database.php"); 
     session_start(); 

     if(isset($_COOKIE['username']) and isset($_COOKIE['passcode'])) 
     { 
      $username = $_COOKIE['username']; 
      $passcode = $_COOKIE['passcode']; 

      $_SESSION['name'] = $username; 
      $_SESSION['pwd'] = $passcode; 

      header("Location: useraccount.php"); /*Checks if cookies are set then takes me directly to useraccount.php */ 

     } 

     else if(isset($_POST['login'])) 
     { 
      $username = $_POST['username']; 
      $userpwd = $_POST['userpwd']; 
      $rem = $_POST['remember']; 

      $query = "SELECT * FROM login WHERE userName = '$username' AND Password = '$userpwd'"; 

      $result = mysqli_query($link, $query); 
      $row = mysqli_num_rows($result); 

      if($row > 0) 
      { 
       $_SESSION['name'] = $username; 
       $_SESSION['pwd'] = $userpwd; 

       if (isset($_POST['remember']))  /*if remember me checkbox is checked than it set cookies and it takes me to useraccount.php page. But if I donot check the check box (remember me) than I am where I am on login page. */ 
       { 
        setcookie("username", $username, time()+60*60*7); 
        setcookie("passcode", $userpwd, time()+60*60*7); 
       } 

       header("Location: useraccount.php");  /* It should procees to    useracocunt.php page but it stays on login.php*/ 
      } 

      else 
      { 
       header("Location: login.php");  /*Used this for testing purpose if login fails it takes me to homepage which is working fine*/ 
      } 
      mysqli_close($link); 

     } if (isset($_SESSION['name']) and isset($_SESSION['pwd'])){ 

header("Location: useraccount.php");  /* It should procees to    useracocunt.php page but it stays on login.php*/ 

} 

     ?> 
+0

嗨,我已經做到了,它並沒有幫助,你看到當記得是一個例外,如果用戶檢查它設置了Cookie,他仍然應該登錄到他的帳戶。 –

+0

我添加if語句來檢查會話是否已設置,因此缺少該語句。請嘗試添加此。 –

+0

但請確保在useraccount.php中,您不僅查看cookie,還查看會話... –

1

它幫助我有同樣的問題。我登錄後將其重定向到相同的登錄頁面。沒有設置我的餅乾條件正確