【发布时间】:2015-12-18 23:04:34
【问题描述】:
用户名和密码存储在一个文件中,如果用户名已经存在于文件中,它应该告诉用户他们不能使用该密码并重新加载页面。现在它没有这样做,它直接进入 cmetsWall.php,即使我输入了相同的用户名。我错过了什么?
<html>
<body>
<h1>Please enter your information to create a new login account</h1>
<form method="post">
Login Name:<input type = "text" name = "name" value = "" required><br/>
Password:<input type = "password" name = "pwd" value = "" required><br/>
<input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
</form>
</body>
</html>
<?php
session_start();
if($_POST){
$handle = fopen("accounts.txt", "r");
if(isset($_POST['name'])){
$username = $_POST['name'];
$file = file_get_contents('accounts.txt');
$search = $username . ",";
if(strpos($file, $search) !== FALSE){
echo 'name already used, choose another';
header('Location: '.$_SERVER['PHP_SELF']);
}
fclose($handle);
}
if(isset($_POST['pwd'])){
$password = $_POST['pwd'];
}
$text = $username . "," . $password . "\n";
$fp = fopen("accounts.txt", 'a+') or die("Unable to open file!");
if(fwrite($fp, $text)){
echo "saved";
fclose($fp);
header("Location: commentWall.php");
}
else{
echo "Unable to store credentials.";
header('Location: '.$_SERVER['PHP_SELF']);
}
}
?>
如果你想看cmetsWall.php的代码就问吧,不过我觉得没必要包含。
【问题讨论】:
-
HTML 输出后不能调用
session_start()。把它放在最上面。 -
不幸的是仍然无法正常工作
-
你应该使用数据库。您是否在
accounts.txt上设置了权限,因此并非所有用户都可以访问它?您可能应该在 HTML 之前拥有所有这些代码。 -
@user3572515 这不是一个解决方案,而是一个不相关的评论。我已经在答案中发布了解决方案。
-
A db 是一个明显的改进,但这只是一个简单的 php 分配,所以我认为没有必要。我没有设置权限,应该是什么?