PHP - 密码重置表单不起作用


PHP - Password Reset form not working

我找到了一个可用于我的网站的教程密码重置功能,所以我下载了文件并玩了代码。我不太喜欢他们的,所以我几乎删除了所有内容并保留了重要的部分。无论如何,我从这里下载了它,不幸的是现在它不起作用。

每当我输入一封电子邮件时,无论它是什么,它都会说"无效的电子邮件"。任何帮助将不胜感激。另外,我是PHP的一个小菜鸟,但我理解这里的所有代码。我的代码如下:

<?php include "base.php"; ?>
 <html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="style2.css" type="text/css" />
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Forgot Password</title>
</head>
<body>
    <div id="main"><h1>Forgot Password</h1>
    <p>If you forgot your password, please enter the email associated with your account, and we will send you a reset link.</p>
        <form action="" method="post" name="passwd" id="passwd">
        <fieldset>
            <label for="emailadd"><div>Email Address:</div></label><input id="emailid" name="emailid" type="text" placeholder="Your Account's Email"><br>
            <input type="submit" value="Reset Password" />
        </fieldset>
        </form>
        </div>
<?php
    if(!empty($_POST['emailid']))
    {
        $emailaddress = mysqli_real_escape_string($_POST['emailid']);
        if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $emailaddress))  // Validate email address
       {
            echo '<p id="msg"><center>That is an invalid email address. Please try again.</center></p>';
        }
        else
        {
            $query = "SELECT UserID FROM users where  EmailAddress='".$emailaddress."'";
            $result = mysqli_query($query);
            $Results = mysqli_fetch_array($result);
           if(count($Results)==1)
            {
                $encrypt = md5(XXXXXXXXXXXXXXXXXXX);
                echo '<p id="msg"><center>Your password reset link has been sent to your email.</center></p>';
                $to=$emailaddress;
                $subject="Forgot Password";
                $from = 'no-reply@XXXXXXXXXXXX.com';
                $body='Hello, <br/> <br/>Your Membership ID is:   '.$Results['id'].' <br><br>Click <a href="http://XXXXXXXXXXX.com/home/forgot/reset.php?encrypt='.$encrypt.'&action=reset">here</a> to reset your password, or follow the link below:<br><br>http://XXXXXXXXXXXX.com/home/forgot/reset.php?encrypt='.$encrypt.'&action=reset <br/> <br/>--<br>XXXXXXXXXXXX<br>Games, Tools, and so much more!';
                $headers = "From: " . strip_tags($from) . "'r'n";
                $headers .= "Reply-To: ". strip_tags($from) . "'r'n";
                $headers .= "MIME-Version: 1.0'r'n";
                $headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
                mail($to,$subject,$body,$headers);
            }
            else
            {
                echo '<p id="msg"><center>Account not found. Please try again.    </center></p>';
            }
        }
    } elseif(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
        echo "<meta http-equiv='refresh'              content='0;http://www.XXXXXXXXXXXX.com/home/main.php'/>";
    } else {
    }
//    include("html.inc"); 
?>
</body>
</html>

P.S. Base.php连接到我的数据库,X代表我的加密或我的网站地址。

提前感谢!

以下函数使您失败:

  • mysqli_real_escape_string($_POST['emailid'])
  • mysqli_query($query)

这两个函数都需要将数据库连接传递给它并作为第一个参数。

引用:

  • http://php.net/manual/en/mysqli.real-escape-string.php
  • http://php.net/manual/en/mysqli.query.php

还要确保您确实启动了会话,看到您正在使用它们。

参考:

  • http://php.net/manual/en/function.session-start.php

脚注:

如果您在该教程链接中注意到您说您遵循了 http://www.phpgang.com/how-to-create-forget-password-form-in-php_381.html

它们正在传递与这两个函数的连接:

$encrypt = mysqli_real_escape_string($connection,$_GET['encrypt']);

$result = mysqli_query($connection,$query);

因此,您需要按照该教程进行">T">,这是您没有做的事情。

这样做,并修改您的数据库以适应该教程的代码和列类型、适当的长度等。

哦,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 将以静默方式失败。

将错误报告添加到文件顶部,这将有助于查找错误

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code

旁注:显示错误只能在暂存中完成,而不应在生产中完成。

以及or die(mysqli_error($connection)) mysqli_query().

您没有向我们展示的是您使用哪个MySQL API进行连接。

  • http://php.net/manual/en/function.mysqli-connect.php

"P.S. Base.php连接到我的数据库,X代表我的加密或我的网站地址。

确保您使用的是MySQLi API进行连接,看到您使用的是mysqli_query()而不是mysql_或PDO。

  • 不同的 API 不会混合在一起。

请咨询以下内容:

  • 我可以在PHP中混合MySQL API吗?
  • http://php.net/manual/en/mysqlinfo.api.choosing.php

您还需要确保邮件可供您使用。

如果不是,那么这超出了这个问题的范围。

改变

mail($to,$subject,$body,$headers);

if(mail($to,$subject,$body,$headers)){
   echo "Mail sent";
}
else {
   echo "Error";
}

看到"邮件已发送",将意味着mail()已经完成了它的工作。

如果您看到"错误",则需要找出"原因"。

再次,问题的"超出范围"。