2014-09-03 50 views
1

如何記錄登錄條目和會話狀態?我搜索源代碼和想法,但我不明白其中的一些。我想通過PHP問一個簡單的代碼。我有一個可以登錄用戶但不需要MySql數據庫的代碼,並且我想知道如何記錄連接我的PHP登錄代碼的LOGIN ENTRIES和SESSION STATE。或者如果您有其他需要MySql數據庫的選項代碼。記錄登錄條目和會話狀態

下面的代碼:

「CONFIG.PHP」

<?php 
$user = "admin"; 
$pass = "password"; 
?> 

「的index.php」

<?php 
include("config.php"); 

// Check form if is submited 
if(isSet($_POST['trimite'])) { 
// Check if user is equal with username and password from config.php 
if($_POST['user'] != $user || $_POST['pass'] != $pass) { 
echo "Sorry, your data is invalid"; 
} else { 
// Open the session for store user logged 
session_start(); 
// Setting the session 
$_SESSION['logat'] = "da"; 
// Redirecting user to admin page if is logged 
Header('Location: admin.php'); 
} 
} else { 
// Form 
echo '<form action="" method="post"> 
Username: <input type="text" name="user"> 
Password: <input type="password" name="pass"> 
<input type="submit" name="trimite"> 
</form>'; 
} 
?> 

「admin.php的」

<?php 
include("config.php"); 
// Start session 
session_start(); 

// Check if user is logged and existing session 
if(isset($_SESSION['logat'])) { 
// Content for user logged 
echo "Welcome ".$user." :) - <a href='logout.php'>Logout</a>"; 
} else { 
// Redirecting to login page 
Header("Location: ./"); 
} 
?> 

回答

1

始終把作爲非常冷杉的session_start() st語句<?php

即使用戶未登錄,也可以運行session_start()session_start()應該是第一條語句。

請注意,header()命令需要小寫h(而不是Header - 這是錯誤的)。

的index.php

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

    // Check form if is submited 
    if(isSet($_POST['user'])) { 
     // Check if user is equal with username and password from config.php 
     if($_POST['user'] != $user || $_POST['pass'] != $pass) { 
      echo "Sorry, your data is invalid"; 
     } else { 
      // Open the session for store user logged 
      // Setting the session 
      $_SESSION['logat'] = "da"; 
      $_SESSION['username'] = $_POST['user']; 
      // Redirecting user to admin page if is logged 
      header('Location: admin.php'); 
     } 
    } else { 
     // Form 
     $out = ' 
      <form action="" method="post"> 
       Username: <input type="text" name="user"> 
       Password: <input type="password" name="pass"> 
       <input type="submit" name="trimite"> 
      </form> 
     '; 
     echo $out; 
    } 
?> 

admin.php的 - 這裏是如何引用/使用的用戶名會話變量:

<?php 
    // Start session 
    session_start(); 

    include("config.php"); 

    // Check if user is logged and existing session 
    if(isset($_SESSION['logat'])) { 
     // Content for user logged 
     echo "Welcome ".$_SESSION['username']." :) - <a href='logout.php'>Logout</a>"; 
    } else { 
     // Redirecting to login page 
     header("Location: ./"); 
    } 
?> 

注意header方能如果尚未向DOM發送數據,則使用此功能。有時候這很難防止。下面是一個HTML標籤,可以讓你重定向到另一頁:

<meta http-equiv="refresh" content="0;url=http://example.com"> 

數爲零(前url=意味着我會在哪裏放秒重定向的頁面之前要等待數

+0

? – Macoi 2014-09-04 08:06:35

+0

它是HTML - 所以你可以把它放在你的HTML代碼中(任何你可以放的地方,例如任何其他的HTML標籤),或者你可以在PHP代碼塊中回顯它:'echo「」;'測試一下,試驗一下,注意很多人會告訴你不要使用這個指令,與任何事情)謹慎和儘可能多地學習,可以讓你做很多事情。「每個人」並不總是對的。 :) – gibberish 2014-09-04 16:40:20

+0

如何記錄用戶的條目和會話狀態的日誌?用戶現在可以登錄,但我仍然需要有一個代碼,用於在用戶註銷後記錄用戶的登錄條目。什麼是會話狀態? session_start()會話狀態碼? – Macoi 2014-09-05 01:45:59