【发布时间】:2015-04-21 06:13:27
【问题描述】:
我正在尝试制作用户注册脚本。
在我的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
当他们点击链接时,他们会被定向到我的 verify.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 脚本注释掉,然后先取消注释并单击用户注册时发送的链接。这行得通。
- 我尝试将我的 verify.php 脚本移动到另一个文件夹。没有任何帮助
- 我尝试在verification.php 中关闭连接,然后创建了一个新的PDO。这也不起作用。
- 我尝试了很多方法来更改 $_GET 方法,但都没有成功。
更新!:现在我试图查看代码中断的确切位置,我注意到一些不寻常的地方。运行registration.php 时,用户在数据库中设置为不活动。一旦我收到带有链接的电子邮件。用户被设置为活动,无需点击链接
请告诉我外面有人知道发生了什么。
【问题讨论】:
-
第一个用户被激活后。它的验证值为空。下一个到达验证页面的用户,其验证码未设置(即为空)。执行以下查询:select from users where verification = null。这将返回第一个用户。
-
是的,但在第二个用户之前,他使用了verification.php,他已经注册了一个帐户,因此从电子邮件中获得了一个他使用的新验证码?
-
你没有在第一个脚本中告诉我们
$Id应该来自哪里。 -
很抱歉我没有包含它,因为我知道该部分工作正常,用户总是被注册。它来自 html 表单中的 $_POST 函数
-
哦,对不起,这不正确,这是所有用户信息。 Id 源自我的 mysql 数据库,我使用 sql SELECT * FROM user WHERE email=:email 因为电子邮件始终是唯一的