2010-11-11 171 views
1

所以我想創建一個登錄腳本爲我的網頁使用PHP和MySQL。我一直在尋找關於這方面所有信息的良好教程的互聯網,但它似乎大多數只是給一個腳本,並在這裏說,你去。問題是我需要更多關於會話和cookie的信息。有沒有人知道一個很好的地方瞭解這個東西?PHP登錄問題

回答

1

老實說,我不知道你想要什麼地方因爲我自己學到了。因爲真的很容易。

所有這一切的基本原則是驗證輸入的登錄信息是否正確(如果您知道我在說什麼,請通過您的BL和DA進行檢查),然後將有關用戶的任意信息存儲在$ _SESSION變量像$_SESSION['name'] = 'guest',然後在每個頁面的開始寫一個簡單的語句,需要登錄來檢查$ _SESSION數組的內容,如果沒有設置,重定向到另一個頁面等等...
這就是它! :D 希望我能回答你要找的東西! ; - ]

編輯: 下面是一個簡單的登錄頁面:

<?php 
session_start(); //required if you want to use the $_SESSION array 
$username = $_REQUEST['txtUsername']; //storing the value of the "txtUsername" text box in the form sent by client after the form has been submited 
$password = $_REQUEST['txtPassword']; //same as $username 

if (isset($username) && isset($password)) 
{ 
    /* 
    * this section depends on the implementation of your BL and DA layers. 
    * assume that my BL selects a member by it's username and returns an array 
    * containing his/her info 
    */ 
    require_once('mybl.php'); //assume that my BL tier is implemented in the "mybl.php" file 
    MyBL $bl = new MyBL(); 
    $queryResult = $bl->selectByUsername($username); 

    //authenticating user 
    if ($queryResult['username'] == $username && $queryResult['password'] == $password) 
    { 
     //the user has been authenticated and can proceed to other pages available for members only 

     /* 
     * i'm storing the user's username in the session so that I could refer to it in other pages 
     * in case I want to update/modify database tables for this specific user. 
     */ 
     $_SESSION['username'] = $username; 
     //store anything else you want for the current user: 
     $_SESSION['name'] = $queryResult['name']; //for welcome prompt 

     header('Location: welcome.php'); //redirecting the user 
    } 
    else //in case of wrong username/password 
    { 
     $message = "Incorrect username/password"; 
    } 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>LoginPage</title> 
</head> 

<body> 
<form method="post" action="login.php"> 
<table style="border: 1px dashed"> 
    <tr> 
     <td>Username:</td> 
     <td><input name="txtUsername" type="text" /> 
    </tr> 
    <tr> 
     <td>Password:</td> 
     <td><input name="txtPassword" type="password" /> 
    </tr> 
    <tr> 
     <td colspan="2" align="center"><input name="btnSubmit" type="submit" value="Login" /> 
    </tr> 
</table> 
</form> 
<span style="color: red;"><?php echo $message; ?> </span> 
</body> 
</html> 

歡迎頁面

<?php 
session_start(); 
//checking to see if the user is logged in 
if (!isset($_SESSION['username'])) 
    header('Location: login.php'); //the user is not logged in, he/she is redirected to the login page. 

/* 
* Basically you might want to put this code at the beginning of every page that requires 
* logging in. This way a user that hasn't logged in can't see the contents of the page 
* and you don't have to worry about extra checking and conditions that could raise errors 
* due to empty "$_SESSION" array. 
*/ 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Login successful</title> 
</head> 

<body> 
Login successfull!<br /> 
Welcome <?php echo $_SESSION['name']; ?> 
</body> 
</html> 
+0

這就是我想學的東西。你沒有使用任何東西來幫助學習它? – shinjuo 2010-11-11 19:58:00

+0

只是由他人寫的一些簡單的代碼。沒有什麼複雜的。如果你想 – M2X 2010-11-11 19:59:43

+0

這將是優秀的,也許你如何將它張貼在每個頁面上的描述,我可以張貼的示例代碼。我知道如何使用MySql並填充它的會話和登錄我不確定 – shinjuo 2010-11-11 20:03:43

1

關於會話和cookie,你到底在尋找什麼?

請確保您使用http://php.net/manual/en/index.php

但是,如果你提供更多的信息,我們可以幫助一點,你在一個更好的方向

編輯: 這看起來像你需要的所有功能相當全面的教程: http://www.evolt.org/node/60265

+0

我只是想創建一個登錄 – shinjuo 2010-11-11 19:54:54

+0

好,請看我上面的編輯。 – Bill 2010-11-11 19:56:11

1

這裏有一些參考資料,可以幫助:

+0

會話和東西是創建登錄頁面的好方法嗎? – shinjuo 2010-11-11 19:55:23

+0

會話是跟蹤用戶身份驗證狀態的好方法,是的。 PHP本身將處理設置會話跟蹤cookie。你只是看起來是一個本地數組,你可以轉儲和獲取你需要跟蹤的任何東西。請注意,此數據不會**發送到客戶端,因此將密碼等信息存儲在會話數組中是相當安全的。你不應該在cookies中存儲這樣的敏感信息。 – cdhowie 2010-11-11 19:58:30

1

瞭解數據庫操作有關PDO。這不僅適用於登錄腳本,還適用於PHP。它將會幫助您編寫安全的程序。

+0

好吧,我會把它放在我的任務清單 – shinjuo 2010-11-11 20:04:40

1

我認爲你遇到的問題是一個安全的登錄腳本發揮了MySQL和PHP的許多功能,這不僅僅是很多教程想要涉及的。

有一個很好的教程here,但它的成立,因此顯示瞭如何構建登錄程序都在PHP4和PHP5,所以觀看數字化的標題,並確保您只使用適用的文件到您的版本PHP。請務必查看有關加密密碼的部分。現在,你一定會做得更好。關於本教程的好處是,它包括每一步的意見,這樣你就會知道他們在做什麼,如果你需要它可以搜索更多信息。如果您需要關於特定部分的解釋,您可以做的另一件事是在這裏發佈一部分腳本。

2

你必須明白,每一次使用在session_start();功能,您在您的服務器上創建一個新的會話,或者如果它已經創建它使一個參考,因此通過瀏覽器在這種識別每一位旅客,並在同一時間,你創建這個會話的服務器設置在報頭中的cookie變量與變數稱爲PHPSESSID這個變量有一個與你的會話的ID散列。

在PHP中,您有一個名爲$ _SESSION的預定義全局變量,這個變量可以用來爲您的登錄存儲數據,例如標誌,就像這樣。

$ _SESSION ['login'] = true;

所以你可以使用這個變量來檢查用戶是否已經登錄過,每次你需要顯示一些只允許註冊用戶的東西。

+0

我忘了告訴你,session_destroy();函數應該用它來註銷,它會結束你的會話。 – alejandro 2010-11-11 20:18:54