2016-06-16 36 views
-3
我和我的login.php頁面無法登錄的問題

,我不能工作了,爲什麼它不斷刷新頁面,每當我嘗試登錄。我正在使用我的index.php直接重定向到我的login.php不知道是否這是問題,因爲我改變它這是工作。有任何想法嗎?登錄php代碼保持清爽?

的index.php

<?php 
    header("Location: Login.php"); 
?> 

的login.php

<?php 
ob_clean();session_start(); 

if (isset($_GET['logout'])){ 
    session_destroy(); 
} 

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){ 
    header("Location: index.php"); 
} 

$Username = $_POST['username']; 
$EnteredPassword = $_POST['password']; 

if (isset($_POST['submit'])){ 
    if (is_dir("USERS/".$Username) === true){ 
     $myFile=fopen("USERS/".$Username."/Password.txt","r") or exit("Can't open file!"); 
     $CorrectPassword = fgets($myFile); 
     fclose($myFile); 

     if ($CorrectPassword == $EnteredPassword){ 
      $_SESSION['loggedin'] = true; 
      header("Location: Home.php");  
     } 

     else { 
      echo '<font color="#FF0000"><p align="center">Username or Password incorrect please try again</p></font>'; 
     } 
    } 

    else { 
     echo '<font color="#FF0000"><p align="center">Username or Password incorrect please try again</p></font>'; 
    } 
} 
?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 

     <title>Project Archive - Login</title> 

     <link href="CSS/boilerplate.css" rel="stylesheet" type="text/css"> 
     <link href="CSS/master.css" rel="stylesheet" type="text/css"> 
     <script src="JAVASCRIPT/respond.min.js"></script> 
</head> 
<body link="black"> 
     <div class="gridContainer clearfix"> 
      <div id="headerLoginDiv"> 
       <div id="titleLoginDiv"> 
        <p>Project Archive</p> 
       </div> 
      </div> 

      <h1 align="center">Login</h1> 
      <h3 align="center">Welcome. Please login to continune.</h3> 
      <form method="post" action="index.php"> 
      <div id="userNameLoginDiv"> 
       <p align="center">Username:</p> 
       <input type="text" name="username" size="12"> 
      </div> 

      <div id="userPasswordLoginDiv"> 
       <p align="center">Password:</p> 
       <input type="password" name="password" size="12"> 
      </div> 

      <div id="loginBtnDiv"> 
       <input id="button" name="submit" type="submit" value="Login"> 
      </div> 
     </form>    
    </body> 
</html> 

回答

2

很顯然,如果你去index.php,你問的瀏覽器去login,不論是否用戶是否登錄:

<?php 
    header("Location: Login.php"); // This blindly redirects the user to the login page. 
?> 

,而不是上面的代碼,檢查併發送用戶:

<?php 
    // Start the session. 
    session_start(); 
    // Instead check if the user is logged in and then redirect. 
    if (!isset($_SESSION['loggedin'])) 
     header("Location: Login.php"); 
?> 

而且,不要忘了在開始與session_start()啓動會話。

+0

在腳本開始時不需要'session_start()'嗎? –

+1

@PraveenKumar感謝您的建議'#same_name' ... :) –

+0

@PraveenKumar權。我們提到我們,涉及'#Mentionception' –