douyinliu8813 2014-03-21 19:14 采纳率: 100%
浏览 12
已采纳

通过管理页面创建新的登录帐户 - 将表格数据保存到文本文件

As you can probably guess, I am new to learning PHP and have an assignment to create a page where new user accounts can be created by entering the username and password into form fields, which then needs to be saved in a text document.

I have spent hours trying to figure this out and have tried what I have seen suggested on other questions. As I mentioned previously, I am very new to this and need to keep things basic.

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $newUsername = trim(htmlspecialchars($_POST["newuser"]));
    $newPassword = trim(htmlspecialchars($_POST["newpwd"]));
    $file = "user_accounts.txt";

    $openfile = fopen($file, "a") or die("Unable to open file");

    if($newUsername || $newPassword == ""){
        echo '<script language="javascript">';
        echo 'alert("Error: Please complete all fields")';
        echo '</script>';}

    else{
        fwrite ($openfile, "$newUsername 
 $newPassword 
");
        fclose($openfile);}

}
?>

And here is my form code:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">        
    Username: <input type="text" name="user"><br> 
    Password: <input type="password" name="pwd"><br> 
    <input type="submit"> 
</form>
  • 写回答

5条回答 默认 最新

  • dongxinjun3944 2014-03-21 19:33
    关注

    The problem with your code is this:

    if($newUsername || $newPassword == "") is missing == "" for $newUsername

    Plus, your input form element names don't match.

    $_POST["newuser"] with name="user" does not match.

    $_POST["newpwd"] and name="pwd" does not match.

    Tested:

    <?php
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $newUsername = trim(htmlspecialchars($_POST["newuser"]));
        $newPassword = trim(htmlspecialchars($_POST["newpwd"]));
        $file = "user_accounts.txt";
    
        $openfile = fopen($file, "a") or die("Unable to open file");
    
        if($newUsername == "" || $newPassword == ""){
            echo '<script language="javascript">';
            echo 'alert("Error: Please complete all fields")';
            echo '</script>';}
    
        else{
            fwrite ($openfile, "$newUsername 
     $newPassword 
    ");
            fclose($openfile);}
    
    }
    ?>
    
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">        
        Username: <input type="text" name="newuser"><br> 
        Password: <input type="password" name="newpwd"><br> 
        <input type="submit"> 
    </form>
    

    Footnotes:

    I suggest you change this line:

    fwrite ($openfile, "$newUsername 
     $newPassword 
    ");
    

    to

    fwrite ($openfile, "$newUsername
    $newPassword 
    ");
    

    because it is adding a space (when testing) at the end of the user name and at the beginning (and at the end) of each password in the file itself, and this could have consequences in regards to character matching.

    fwrite ($openfile, "$newUsername 
     $newPassword 
    ");
                                    ^               ^
    

    Another thing is, using this type of system is considered unsafe unless you are properly hiding it outside of the public realm and/or using .htaccess

    Using a DB is safer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)