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 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线