我的登录页面有问题。我仍然是php的新手,我的老师告诉我使用php创建一个登录页面,你从文本文件中读取你的用户名和密码..如果用户名是正确的,那么应该转到下一页,如果没有用户名,它将会打印输出说没有用户名。
请帮助..
这些是我的login.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="logg.php" method="POST">
<br /> Name: <input type="text" name="uname"><br /> Password: <input
type="password" name="password" size="15" maxlength="30" /> <br /> <input
type="submit" name="login" value="Post!"> <br />
</form>
</body>
</html>
这些是我的login.php
<?php
if (isset($_POST['login'])){
$myFile = "accounts.txt";
$myFileLink = fopen($myFile, 'r');
$myFileContents = fread($myFileLink, filesize($myFile));
fclose($myFileLink);
echo $myFileContents;
?>
答案只显示了所有的名字,但我的老师要求我做的只是阅读某些名字..而且我不知道该怎么做..请帮忙
这应该是这样的:
答案 0 :(得分:0)
您可以使用正则表达式在.txt
中查找特定字符串(username = Bob)http://php.net/manual/en/function.preg-match.php
或者您可以尝试使用反序列化
// set data for example
$store = array(
'username' => 'Bob',
'password' => 'YouShouldNotPass'
);
$fp = fopen('pass.txt','w');
fwrite($fp,serialize($store));
// Reading the data
$passtxt = file_get_contents('pass.txt');
$info = unserialize($passtxt);
echo $info['username'];
或者你可以试试json: http://php.net/manual/en/function.json-decode.php
答案 1 :(得分:0)
我希望它会给你一些直觉,告诉你如何在登录表单提交后阅读特定的用户名和匹配。
$myFile = "accounts.txt";
//$string = file_get_contents($myFile); //you've to read it like this
$string = 'Hanna,2302 Lala,ha2302'; // I assume you have this data on file
$username_password = explode(' ',$string);
foreach($username_password as $u_p){
list($username,$password) = explode(',',$u_p); //map it to 2 different variable
// you can add any kind of condition to match specific user after login
echo "username = $username & password = $password <br/>";
}
修改强>
<?php
if (isset($_POST['login'])){
$myFile = "accounts.txt";
//$string = file_get_contents($myFile); //you've to read it like this
$string = 'Hanna,2302 Lala,ha2302'; // I assume you have this data on file
$username_password = explode(' ',$string);
foreach($username_password as $u_p){
list($username,$password) = explode(',',$u_p);//map it to 2 different variable
// you can add any kind of condition to match specific user after login
echo "username = $username & password = $password <br/>";
}
}
?>