收到来自 Xampp 的错误消息通知:未定义的索引 [php]


Got error message from Xampp Notice: Undefined index [php]

我从我的 xampp 服务器收到一条错误消息,说:

Notice: Undefined index: u_name in C:'xampp'htdocs'kownload'path'admin-rgst.php on line 5
Notice: Undefined index: u_pwd in C:'xampp'htdocs'kownload'path'admin-rgst.php on line 6
Notice: Undefined index: u_email in C:'xampp'htdocs'kownload'path'admin-rgst.php on line 7

我的php代码:

<?php
    include 'header.php';
    ?>
    <?php
$u_name = strip_tags($_POST['u_name']);
$u_pwd = md5($_POST['u_pwd']);
$u_email = strip_tags($_POST['u_email']);
if(isset($_POST['rgstdo'])){
    if(!empty($u_name) or !empty($u_pwd) or !empty($u_email)){
        echo "<h1>Error</h1>";
        }
}
?>

我的网页 :

 <form action="admin-rgst.php" method="post">
    <table>
<tr>
<td><input type="text" name="u_name" placeholder="username"></td>
</tr>
<tr>
<td><input type="password" name="u_pwd" placeholder="pwd"></td>
</tr>
<tr>
<td><input type="email" name="u_email" placeholder="email"></td>
</tr>
<tr><td><input type="submit" name="rgstdo" value="submit"></td></tr>
    </table>
    </form>

再次PHP

<?
    include 'footer.php';
    ?>

表单正在工作(显示(,空错误消息也有效但是我在标题中有这些消息。请问问题和解决方案是什么?

你的逻辑是错误的。首先检查是否按下了提交,然后检查是否有任何输入为空。

另外,看起来好像你的整个代码都在同一个文件中,反过来给你这些通知。页面加载后,它会抛出这些错误。

<?php
if(isset($_POST['rgstdo'])){
$u_name = strip_tags($_POST['u_name']);
$u_pwd = md5($_POST['u_pwd']);
$u_email = strip_tags($_POST['u_email']);
if(!empty($u_name) or !empty($u_pwd) or !empty($u_email)){
    $u_name = strip_tags($_POST['u_name']);
    $u_pwd = md5($_POST['u_pwd']);
    $u_email = strip_tags($_POST['u_email']);
        }
if(empty($u_name) or empty($u_pwd) or empty($u_email)){
        echo "<h1>Error</h1>";
    }
else{
   echo "All fields were filled.";
   }
}
?>

密码

我还注意到您可能正在尝试使用 MD5 存储密码。不建议这样做,并且已经很旧了,不再被认为是安全的,可以用作密码存储功能。

使用以下方法之一:

  • CRYPT_BLOWFISH
  • crypt()
  • bcrypt()
  • scrypt()
  • 在开墙上
  • PBKDF2
  • PBKDF2 on PHP.net
  • PHP 5.5 的password_hash()函数。
  • 兼容包(如果 PHP <5.5(https://github.com/ircmaxell/password_compat/

其他链接:

  • PBKDF2 for PHP

关于列长的重要旁注:

如果您决定使用password_hash()或加密,请务必注意,如果您当前密码列的长度低于 60,则需要将其更改为该长度(或更高(。手册建议长度为 255。

您需要更改列的长度并使用新的哈希重新开始才能使其生效。否则,MySQL 将以静默方式失败。

尝试如下:

if(isset($_POST["rgstdo"]){
$u_name = strip_tags($_POST['u_name']);
$u_pwd = md5($_POST['u_pwd']);
$u_email = strip_tags($_POST['u_email']);
if(isset($_POST['rgstdo'])){
    if(!empty($u_name) or !empty($u_pwd) or !empty($u_email)){
        echo "<h1>Error</h1>";
        }
}
}