PDO电子邮件验证

时间:2015-02-19 18:32:26

标签: php mysql email pdo

我正在尝试制作用户注册脚本。

在我的registration.php脚本中,我验证用户输入,然后将它们插入数据库。然后,我想使用SMTP向用户发送电子邮件中的验证链接:

$user_activation_hash = sha1(uniqid(mt_rand(), true)); //creating ramdom string

    $mail = new PHPMailer;
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';

    $mail->Host       = "info";         // SMTP server
    $mail->Username   = "info"; // SMTP account username
    $mail->Password   = "info";                 // SMTP account password
    $mail->SMTPAuth   = true;                       // enable SMTP authentication
    $mail->Port       = info;                       // set the SMTP port for the server

    $mail->From       = "info"; //the email the mail comes from
    $mail->FromName   = "someName";                 //what name should be shown at the email
    $mail->AddAddress($email);                      //where the mail should be sent to
    $mail->Subject    = "email validation";         //subject of the mail

    //how the link should look in the mail the "url" should point to the verification.php file
    $link = "url path to my verification.php script".'?verification_code='.urlencode($user_activation_hash);

    //the message in the mail with the above link
    $mail->Body = "Please click on this link to activate your account:".' '.$link;

    if(!$mail->Send()) {
        echo "there was an error sending the mail" . ' ' . $mail->ErrorInfo;

        //if there is an error sending the mail then I delete it here

        return false;

    } else {
        //here I update the user with the new random created string

        $sql = 'UPDATE `user` SET verification = :verification WHERE Id = :Id';
        $stmt = $dbh->prepare($sql);
        $stmt->bindParam(':Id', $Id, PDO::PARAM_STR);
        $stmt->bindParam(':verification', $user_activation_hash, PDO::PARAM_STR);
        $stmt->execute();

        $dbh = null;

        return true;
    }

到目前为止,所有这一切都正常,注册用户会收到一封创建了随机链接的电子邮件。

以下是用户获得的链接示例:http://url/to/verification.php?verification_code=80371b8ff9b0d5fb444f4be68c8b5a0d9757603b

当他们点击链接时,他们将被定向到我的verification.php脚本:

if(!empty($_GET['verification_code']) && isset($_GET['verification_code'])){
$verificationCode = $_GET['verification_code'];

//check the database for the verification code from the link
$sql = 'SELECT Id, verification FROM `user` WHERE verification = :verification AND isActive = 0';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':verification', $verificationCode, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch();

$Id = $row['Id'];

if (empty($row)){
        echo "the account was not found";
    }else{
        //if they match. make the user active in db
        $sql = 'UPDATE user SET isActive = 1, verification = NULL WHERE Id=:Id';
        $stmt = $dbh->prepare($sql);
        $stmt->bindParam(':Id', $Id, PDO::PARAM_STR);
        $stmt->execute();
        $row = $stmt->fetch();

        echo "The account has been activated!";
    }

}
}

好的,所以这是我的头痛,我希望我能正确解释:

所有这一切都有效。当我创建第一个用户后,它可以在注册后工作,我可以在数据库中看到验证码,当我点击链接时,它会被激活。但是当我点击registration.php脚本时,正在激活以下用户注册!这就像两个脚本一次运行,然后完全没必要激活链接。

我不知道导致这种行为的原因。是因为我的pdo连接没有从我的第一个脚本中正确关闭吗?是因为当我只调用一个脚本时,PHP通常只运行目录中的所有脚本吗?是因为我不明白$ _GET函数是如何工作的?

我无法找到一种方法,为什么这不应该工作所以这里有一些我已经尝试过的事情:

  • 我已尝试使用已注释的verification.php脚本注册用户,然后首先取消注释,然后单击用户注册时发送的链接。这很有效。
  • 我尝试将我的verification.php脚本移动到另一个文件夹。没有任何帮助
  • 我尝试在verify.php中关闭连接,然后创建了一个新的PDO。这也没有用。
  • 我尝试了很多方法来改变$ _GET方法,但没有成功。

更新!:现在我试图看到代码中断的确切位置,我发现了一些不寻常的东西。当registration.php运行时,用户在数据库中设置为不活动。只要我收到带有链接的电子邮件。用户设置为活动状态,无需单击链接

请告诉我那里的人知道了什么。

1 个答案:

答案 0 :(得分:1)

问题是注册表时第一个用户是完全空的 但是当第二个用户注册并输入没有获取值的verification.php时,它会搜索具有verify = null(第一个用户)的用户 并完成代码,所以您只需要修改代码

只需编辑verify.php文件中的第一个查询,而不是此

$sql = 'SELECT Id, verification FROM `user` WHERE verification = :verification';

这样做

$sql = 'SELECT Id, verification FROM `user` WHERE verification = :verification AND isActive = 0';

用于检查是否发送了值

if(isset($_GET["verification_code"]){
    $sql = 'SELECT Id, verification FROM `user` WHERE verification = :verification AND isActive = 0';
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':verification', $verificationCode, PDO::PARAM_STR);
    $stmt->execute();
    $row = $stmt->fetch();

    $Id = $row['Id'];

    if ($Id == null){
        echo "the account was not found";

    }else{

// check if the verificationcode found in the database, matches the verificationcode from the link
if ($row['verification'] !== $verificationCode) {

    //checking if it already exists and if there is an error then deleting the user

} else {

    //if they match. make the user active in db
    $sql = 'UPDATE user SET isActive = 1, verification = NULL WHERE Id=:Id';
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':Id', $Id, PDO::PARAM_STR);
    $stmt->execute();
    $row = $stmt->fetch();

    echo "The account has been activated!";

}
}
}