2013-10-06 42 views
3

嗨我已經看了其他文章關於這個,但他們是2歲或更多年,所以我認爲這是最好開始新鮮。來自文本文件的PHP登錄

正如標題所示,我試圖用php做一個登錄頁面。用戶應該能夠登錄到特殊的會員專頁。用戶名和密碼存儲在一個文本文件中(注意,這是爲了一個任務,否則我會使用SQL)。我的代碼如下

?php 

    echo "Username: <input type=\"text\" name=\user-name\"><br>"; 
echo "Password: <input type=\"text\" name=\pass-word\"><br>"; 
echo "<input type=\"submit\" value=\"login\" name=\"login\"><br>"; 

$userN = $_POST['user-name']; 
$passW = $_POST['pass-word']; 
$userlist = file ('users.txt'); 
$checkUser =$userlist[0]; 

if (isset($_POST['login'])) 
{ 
if ($userN == $userlist) 
{ 
    echo "<br> Hi $user you have been logged in. <br>"; 
} 
else 
{ 
echo "<br> You have entered the wrong username or password. Please try again. <br>"; 
} 
} 
?> 
<form action="login.php" method="post"> 
Username: <input type="text" name="username"> 
<br /> 
Password: <input type="password" nme="pass" 
<br /> 
<input type="submit" name="submitlogin" value="Login"> 

我知道我需要使用爆炸功能,我需要定義文本文件將如何被置出。理想的用戶名|密碼。在一個名爲users.txt的文件中用戶文件還必須包含諸如電子郵件(可以替換用戶名),客戶名稱,(客戶的)企業名稱以及會員特殊價格等信息。

回答

1

比方說您的文本文件看起來是這樣的:

pete|petepass|[email protected]|Pete Enterprizes 
john|johnpass|[email protected]|John Corporation 

您的代碼可以讀出這樣的事情:

$userN = $_POST['user-name']; 
$passW = $_POST['pass-word']; 
$userlist = file ('users.txt'); 

$email = ""; 
$company = ""; 

$success = false; 
foreach ($userlist as $user) { 
    $user_details = explode('|', $user); 
    if ($user_details[0] == $userN && $user_details[1] == $passW) { 
     $success = true; 
     $email = $user_details[2]; 
     $company = $user_details[3]; 
     break; 
    } 
} 

if ($success) { 
    echo "<br> Hi $userN you have been logged in. <br>"; 
} else { 
    echo "<br> You have entered the wrong username or password. Please try again. <br>"; 
}