1

我在 PHP 中创建了一个密码重置功能。

它工作得很好............除了,由于某种原因,我无法设置收件人的电子邮件地址:“ TO

代码以这种方式工作:

(a) 要求用户提供他的登录名/用户名 (b) php 向数据库发送一个 sql 查询;(c) 如果找到用户名,php 获取电子邮件地址,并通过电子邮件发送重置链接 (d) 此重置链接附有唯一的“令牌” (e) 用户点击他的链接电子邮件,并被重定向到他重置密码的新页面

一切正常............除了电子邮件结构本身。电子邮件包括: TO、CC、SUBJECT、BODY 和 HEADERS

一切都在显示............除了实际的“ TO ”。

事实上,我知道代码有效的唯一原因是因为我通过“ CC ”获得了电子邮件的副本

这是我的代码:

  if(isset($_POST['submit'])) {

  $login = $_POST['login'];

  $query = "select * from personal_data where login='$login'";
  $result = mysqli_query($conn,$query);
  $count=mysqli_num_rows($result);
  $rows=mysqli_fetch_array($result);


  if($count==0) {

 echo "Sorry; that username does not exist in our database";
 }

else {

 function getRandomString($length) 
   {
$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789!#+=%&/?*$";
$validCharNumber = strlen($validCharacters);
$result = "";

for ($i = 0; $i < $length; $i++) {
    $index = mt_rand(0, $validCharNumber - 1);
    $result .= $validCharacters[$index];
 }
 return $result;    }

$token=getRandomString(40);
$q="insert into token (token,login) values ('".$token."','".$login."')";
mysqli_query($conn,$q);

function mailresetlink($to,$token){

$to = $rows['email'];
$subject = "Password Reset";
$uri = 'http://'.$_SERVER['HTTP_HOST'] ;
$message = '
<html>
<head>
<title>Password Reset Link</title>
</head>
<body>
<p>We received a Password-Reset request from your account.</p>
<p>Click on the following link to reset your password : <a   
href="'.$uri.'/PHP/password_reset?token='.$token.'">Reset Password</a></p>
</body>
</html>
 ';
 $headers = "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
 $headers .= 'From: Support<support@xxxxx.com>' . "\r\n";
 $headers .= 'Bcc: Info<info@xxxxx.com>' . "\r\n";

 if(mail($to, $subject, $message, $headers))     {

  echo "A password reset link has been sent to your email address."

    }
 }

 if(isset($_POST['login'])) { 
    mailresetlink($email,$token);

    exit();
            } 

     }
  }
4

2 回答 2

1

您的代码无法正常工作的原因是由于一些原因。

其中之一是$rows需要驻留在function mailresetlink($to,$token)函数的参数内。

将其更改function mailresetlink($to,$token,$rows)为并对内部执行相同操作if(isset($_POST['login'])){...}

if(isset($_POST['login'])) { 
    mailresetlink($email,$token,$rows);

    exit();
            } 

另外,如果不是拼写错误或粘贴错误;此行中还缺少一个分号:

echo "A password reset link has been sent to your email address."
                                                                 ^ right there

完成以上所有操作后,在我的测试期间成功地将所有信息发送到电子邮件。


旁注:您当前的代码对SQL 注入是开放的。mysqli与prepared statements 一起使用,或与prepared statements 一起使用PDO它们更安全

于 2015-05-11T01:46:55.660 回答
0

您不能在 if 或 while 或任何范围内定义函数。在您打算使用它们之前或之后定义它们。尝试使用以下代码:

<?php
if ( isset($_POST['submit']) ) {

    $login = $_POST['login'];
    $email = $_POST['email'];


    $query = "select * from personal_data where login='$login'";
    $result = mysqli_query($conn, $query);
    $count = mysqli_num_rows($result);
    $rows = mysqli_fetch_array($result);


    if ($count == 0) {

        echo "Sorry; that username does not exist in our database";
    } else {


        if (isset($_POST['login'])) {
            mailresetlink($email, $token, $rows);

            exit();
        }

    }
}

function getRandomString($length)
{
    $validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789!#+=%&/?*$";
    $validCharNumber = strlen($validCharacters);
    $result = "";

    for ($i = 0; $i < $length; $i++) {
        $index = mt_rand(0, $validCharNumber - 1);
        $result .= $validCharacters[$index];
    }
    return $result;
}

$token = getRandomString(40);
$q = "insert into token (token,login) values ('" . $token . "','" . $login . "')";
mysqli_query($conn, $q);

function mailresetlink($to, $token, $rows)
{

    $to = $rows['email'];
    $subject = "Password Reset";
    $uri = 'http://' . $_SERVER['HTTP_HOST'];
    $message = '
        <html>
        <head>
            <title>Password Reset Link</title>
        </head>
        <body>
        <p>We received a Password-Reset request from your account.</p>

        <p>Click on the following link to reset your password : <a
                href="' . $uri . '/PHP/password_reset?token=' . $token . '">Reset Password</a></p>
        </body>
        </html>
        ';
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: Support <support@xxxxx.com>' . "\r\n";
    $headers .= 'Bcc: Info <info@xxxxx.com>' . "\r\n";

    if (mail($to, $subject, $message, $headers)) {

        echo "A password reset link has been sent to your email address.";

    }
}
?>

另外,请注意 Quentin 关于防止 SQL 注入的建议。

我所做的是:

  • 移动getRandomStringmailresetlinkif块之后
  • $rows向函数添加了参数mailresetlink,因此它可以找到$rows变量的使用(超出范围)

您还需要定义$email,因为它没有在任何地方设置,所以我为您做了(我猜您也有一个输入字段,其名称为email某个地方。

测试它,它应该工作。

于 2015-05-10T19:41:13.343 回答