创建登录帐户并将其存储在使用php的文本文件中 [英] Create login accounts and store them in a text file using php

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

问题描述

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

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>

用户在输入框中输入数据后,我想运行一个php脚本以将该数据存储到名为accounts.php的文本文件中(我知道这是不安全的,但该数据对我来说没有任何价值使其成为学习过程的一部分.

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

到目前为止,我有以下php代码将数据存储在文件createaccount.php

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();
?>

我相信这段代码应该从登录名和密码中获取输入,并以以下格式将其存储在名为accounts.txt的文件中

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.

然后在屏幕上回显account created,然后将用户带到我的login.html页面,以便他们可以使用那里的新帐户信息登录.

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.

推荐答案

我只是为了保存而尝试了

I just tried this for saving and it works

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

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

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