我想为我的简单登录网站创建一个创建帐户页面,用户可以在该页面上单击创建帐户按钮然后将它们带到具有以下表单的页面,以输入登录名和密码。
<form action = "createaccount.php" method="get">
<h1> Please enter your information to create a new login account</h1>
<p>
<label>Login Name:</label><input type = "text" name = "name" />
<label>Password:</label><input type = "password" name = "pwd" />
<br/><br/>
</p>
<input type = "submit" id = "submit" value = "submit"/>
<input type = "reset" id = "reset" value = "reset"/>
</form>
在用户将数据输入到输入框中后,我想运行一个php脚本将这些数据存储到一个名为accounts.php
的文本文件中(我知道它不安全,但这些数据对我来说没有价值,因为我正在将其作为学习过程的一部分进行编制)。
到目前为止,我有以下php代码将数据存储在文件createaccount.php
中
<?php
$username = $_GET['name'];
$password = $_GET['pwd'];
$filename = 'accounts.txt';
$fp = fopen($filename, 'a+');
fwrite ($fp, $username . "," . $password . "'n");
$fclose ($fp);
echo ("account created");
header("Location: "login.html");
die();
?>
我认为这段代码应该接受登录名和密码的输入,并将它们存储在一个名为accounts.txt
的文件中,格式如下
username1,password1
username2,password2
etc.
然后回显屏幕CCD_ 4,然后将用户带到我的CCD_。
但我尝试运行代码,它根本不会将我的数据保存到文件中,当我提交表单时,它也不会将我引导回登录屏幕,我只收到一条消息,说页面无法显示。
我刚刚尝试过保存,它可以
<?php
if(isset($_POST['submit_btn']))
{
$username = $_POST['name'];
$password = $_POST['pwd'];
$text = $username . "," . $password . "'n";
$fp = fopen('accounts.txt', 'a+');
if(fwrite($fp, $text)) {
echo 'saved';
}
fclose ($fp);
}
<form action = "" method="POST">
<h1> Please enter your information to create a new login account</h1>
<p>
<label>Login Name:</label><input type = "text" name = "name" />
<label>Password:</label><input type = "password" name = "pwd" />
<br/>
<br/>
</p>
<input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
<input type = "reset" id = "reset" value = "reset"/>
</form>