gpt4 book ai didi

php - 创建注册和登录页面

转载 作者:行者123 更新时间:2023-12-04 05:21:25 25 4
gpt4 key购买 nike

我目前正在制作注册和登录页面。我的第一页询问您是否要创建帐户或登录。我们要做的是从用户输入信息的表单中获取信息,并将其放入文本文件中。我有这个工作粗略。我知道出于安全原因这不是好的做法,但这是类作业,我必须将信息放入文本文件中。我有信息进入文本文件,但是我无法将发布的用户名与数据库中的所有用户名进行比较。这是我的 newaccount.html 页面代码,以及表单提交到的 register.php 文件。

newaccount.html

<html>
<head>
</head>
<body>

<h3>Hello new user! Choose a user name and password ^_^</h3><br>

<form action = "register.php" method = "POST" >
Username: <input type = "text" name = "username"><br><br>
Password: <input type = "password" name = "pass"><br>
<input type = "submit" value = "Create Account" name = "submit"><br>
</form>
<form method = "LINK" action = "Proj2_practice.html">
<input type = "submit" value = "Home Page" name = "submit2">
</form>

</body>
</html>

register.php
<?php

$username = $_POST['username'];
$password = $_POST['pass'];

$userAndPass = $username.",".$password;

$userNames = 'usernames.txt'." ";
$passwords = 'passwords.txt'." ";

$fh_users = fopen($userNames, 'a+')or die("sorry, we couldnt open the file");
$fh_passwords = fopen($passwords, 'a+')or die("sorry, we couldnt open the file");

fwrite($fh_users, $username." ");
fwrite($fh_passwords, $password." ");

$allUserNames = fread($fh_users, filesize('usernames.txt'));

echo $allUserNames;

?>

用户名和密码被正确发送到文本文件,在代码的末尾,它没有回显变量。截至目前,文本文件中没有信息,我不知道这是否是没有回显的原因。我的方法在这里正确吗?我在这里尝试做的是将每个名称和密码发送到用户名和密码文本文件。然后,我计划

通过它们之间的空格分解每个文本文件,这就是为什么我在将它们写入文本文件后添加一个,然后通过它们各自的数组元素比较用户名和密码。我是否像我想的那样把这个问题复杂化了?请与我分享您的意见,我只是想变得更好^_^

提前谢谢你!

最佳答案

如果你用“a+”标志打开文件,它会将文件指针放在文件的末尾。所以当你阅读时,你不会得到任何东西,因为你已经在文件的末尾。

在阅读之前回到文件的开头

fseek ($fh_users, 0);

或使用标志“r”关闭并再次打开它
$fh_users = fopen($userNames, 'r')

或者干脆做
$dataString = file_get_contents($userNames);


$dataArray = file($userNames);

关于php - 创建注册和登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com