【发布时间】:2015-03-17 11:46:16
【问题描述】:
我正在为我的网站创建一个电子邮件激活页面,但遇到了问题。似乎尽管页面接收到显示成功激活消息所需的所有信息,但它随机决定显示错误消息,指出凭据与数据库中的任何内容都不匹配。这是代码
// Check their credentials against the database
try {
$check = $db->prepare("SELECT * FROM users WHERE id= ? AND username = ? AND email = ? AND user_ident = ? AND activated = '1' AND email_activated = '0' LIMIT 1");
$check->bindParam(1, $id);
$check->bindParam(2, $u);
$check->bindParam(3, $e);
$check->bindParam(4, $p);
$check->execute();
$num_rows = $check->rowCount();
} catch(Exception $er){
// ERROR : COULD NOT INSERT EMAIL INTO USERS TABLE
Send_email('support@site.com', 'check user - email activation failure', 'Could not check user '.$er);
exit;
}
// Evaluate for a match in the system (0 = no match, 1 = match)
if($num_rows === 0){
Send_email('support@site.com', 'email activation credentials no match', 'invalid email activation variables.<br/> email: '.$e.'<br/>userid: '.$id.'<br/>username: '.$u.'<br/>user_ident: '.$p);
header("location: message.php?msg=Your credentials are not matching anything in our system");
exit();
}
// Match was found, you can activate them
try {
$checking = $db->prepare("UPDATE users SET email_activated = 1 WHERE id= ? LIMIT 1");
$checking->bindParam(1, $id);
$checking->execute();
} catch (Exception $er){
// ERROR : COULD NOT INSERT EMAIL INTO USERS TABLE
Send_email('support@site.com', 'Email add error', 'Could not set active state '.$er);
exit;
}
因此,有时,当单击电子邮件中的激活链接时,它会正常工作,然后有时它会抛出错误,提示凭据与数据库中的任何内容都不匹配,但是当我在单击后立即检查数据库时链接,email_activated 字段已更新以显示它已被激活。 这在技术上不应该发生,因为如果我这样做正确,页面将在抛出凭据错误后退出,这意味着 email_activated 更新代码甚至不应该运行。 似乎它可能运行了两次,但我使用了一个计数器,并且只显示了一次运行。 任何帮助都会很棒!
【问题讨论】:
-
可能是浏览器正在缓存重定向。第一次访问具有清除缓存的 URL 时会发生这种情况吗?尝试明确指定这是“302 Found”重定向,看看是否有帮助。
-
我刚刚再次对其进行了测试,发现我什至不必在地址栏中按 Enter 即可更新页面。我从电子邮件中复制了激活链接,粘贴在 chrome 地址栏中,然后查看了我的用户表,它更新了 email_activated 列以显示它已被激活..
-
看来您对缓存的预感是正确的。我在激活页面中添加了
header('Expires: Sun, 01 Jan 2014 00:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache');,问题似乎已经停止。 -
Chrome 特别难。我有经验,即使在您清除缓存后,它有时也会缓存重定向。因此,以 Firefox 为例。我添加了一个完整的答案仅供参考。顺便说一句,没有在地址栏中按 Enter 键的东西是“chrome page preloading”功能。如果确定您会打开它,它会预加载页面。
标签: php conditional