从文本文件登录PHP [英] PHP login from text file
问题描述
您好,我看过其他有关此内容的文章,但它们已有2年或2年以上的历史,因此我认为最好重新开始.
Hi I have looked at other posts about this but they are 2 or more years old so I thought it was better to start fresh.
正如标题所示,我正在尝试使用php创建登录页面.用户应该能够登录到特殊的会员专用页面.用户名和密码存储在一个文本文件中(请注意,这是为了进行分配,否则我将使用SQL).我的代码在
As the title suggests I am trying to make a login page with php. Users should be able to login to a special member only page. The usernames and passwords are stored in a textfile (note this is for an assignment otherwise I'd use SQL). My code is below
?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的文件中,用户文件还必须包含信息,例如电子邮件(可以替换用户名),客户名称,(客户的)公司名称以及会员的特价.
I know I need to use the explode function and I need to define how the text file will be set out. ideally username|password. in a file called users.txt The users file also has to contain information such as the email (can replace username) the customers name, the business name (of the customer) and special prices for members.
推荐答案
可以说您的文本文件看起来像这样:
Lets say your text file looks something like this:
pete|petepass|pete@somesite.com|Pete Enterprizes
john|johnpass|john@somedomain.com|John Corporation
您的代码可以读取如下内容:
Your code can read something like this:
$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>";
}
这篇关于从文本文件登录PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!