PHP使用更新SQL激活帐户
好的,我有这个代码发送电子邮件帐户验证链接PHP使用更新SQL激活帐户,php,sql,security,Php,Sql,Security,好的,我有这个代码发送电子邮件帐户验证链接 $verifyemail = $clean['email']; $to = $verifyemail; $subject = 'Virtual Pierz Close | Verify Your Account'; $message = "Thanks for registering with VPC, on clicking the verification link below, your
$verifyemail = $clean['email'];
$to = $verifyemail;
$subject = 'Virtual Pierz Close | Verify Your Account';
$message = "Thanks for registering with VPC, on clicking the verification link below, your account will be confirmed, you can then go ahead buy Virtual Properties, donating £5 each time to the worthwhile charity.
http://www.cambrianvacation.co.uk/vpc/registered.php?
email='$verifyemail'&hash='$hash1' ";
$headers = 'From:noreply@cambrianvacation.co.uk'; // Set from headers
mail($to, $subject, $message, $headers);
然后我有了这段代码,它试图通过在数据库中设置active=1来激活帐户,这将是登录时访问控制逻辑的一部分,如果active=1,除了其他保护之外,没有登录
if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['hash']) && !empty($_GET['hash'])){
// Verify data
$accountemail = $_GET['email'];
$accounthash = $_GET['hash'];
}
$accountActive = 1;
$notactive = 0;
$username = '';
$password2 = '';
$username = 'xxxxxxx';
$password2 = 'xxxxxxx';
$db1 = new PDO('mysql:host=localhost;dbname=xxxxxxxxxxxxx', $username, $password2, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db1->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$search = $db1->prepare("SELECT email, hash, active FROM users WHERE email = :email AND hash= :hash AND active = :active");
$search->bindParam(':email', $accountemail);
$search->bindParam(':hash', $accounthash);
$search->bindParam(':active', $notactive);
$search->execute();
$colcount = $search->columnCount();
}catch(PDOException $e) {
$e->getMessage();
}
print_r($colcount);
if($colcount === 3){
//try{
$update = $db1->prepare("UPDATE users SET active=:active WHERE email=:email AND hash=:hash AND active = :active");
$update->bindParam(':active', $accountActive);
$update->bindParam(':email', $accountemail);
$update->bindParam(':hash', $accounthash);
$update->bindParam(':active', $notactive);
$update->execute();
//}catch(PDOException $e) {
// $e->getMessage();
//}
但是,我无法更新活动列
我还考虑过使用GET['email']可能会受到语义url攻击,但是如果没有匹配的哈希,逻辑将无法激活帐户,该哈希是通过crypt()随机生成的。。。。。。。。。
如果有人能看到代码中的任何安全漏洞,请告诉我 新参数绑定不正确,请更改:
$update = $db1->prepare("UPDATE users SET active=? WHERE email=? AND hash=? AND active = ?");
致:
编辑-完整更新代码:
$update = $db1->prepare("UPDATE users SET active=:active WHERE email=:email AND hash=:hash");
$update->bindParam(':active', $accountActive);
$update->bindParam(':email', $accountemail);
$update->bindParam(':hash', $accounthash);
$update->execute();
你能做的,是根本不包括“电子邮件” 您可以尝试通过以下操作生成url:
$secret = "1032940fdjsjdkf#@$!@#%djsfisd";
$hash = md5($email.$secret);
$url = "http://www.cambrianvacation.co.uk/vpc/registered.php?hash=".$hash;
这里真的没有理由进行两个单独的查询。为什么不使用一个查询来根据哈希和电子邮件更新记录,并且active=0?如果修改行数=1,则表示您成功,否则表示您失败。您可能不关心它为什么失败,因为从安全角度来看,向用户指出更新失败的原因(即坏电子邮件、坏散列、已经处于活动状态的用户等)是不好的 话虽如此,您的问题实际上在于更新使用
?
样式绑定,而您使用bindParam()
和:param
样式绑定。这将不起作用,因为这些值在准备好的语句中不存在
因此,只需使用一个查询:
UPDATE users SET active = 1 WHERE email = :email AND hash = :hash AND active = 0
显然,如果您认为要更改active/non-active的值,那么也可以随意为这些值使用参数,但我的猜测是您希望将其视为仅允许值为0和1的布尔型tinyint字段,因此在那里进行参数化是没有意义的。在更新查询中,您正在使用“”作为参数,但随后尝试将它们设置为使用bindParam()命名。你应该使用
$update->execute(array($accountActive, $accountemail, $accounthash, $notactive));
或者通过以下方式修改更新查询:
$update = $db1->prepare("UPDATE users SET active=:active WHERE email=:email AND hash=:hash");
是的,我已经更改了,很抱歉发布了错误的代码,因为我一直在尝试不同的事情…:-/@AntPower对于您现在显示的代码,您想将上一个
?
更新为:散列,并删除和active=active
^(您无需再次检查以确保它们尚未激活,就像您在初始select查询中所做的那样)(编辑:同时删除$update->bindParam(':active',$notactive);
,您现在正在重置参数,因此它将更改/保持为非活动状态)我已经尝试了两种方法,也没有更新mySql数据库中的活动列,我更喜欢使用bindParam…如果删除注释最后一个try/catch组的斜杠,它是否会给您任何错误?也许您可以理解发生了什么。我想知道您是否需要在更新查询之前关闭光标,使用$UPDATE->closeCursor();请停止编辑问题而不告诉任何回答您的人,这会使您很难尝试、帮助和参考您所拥有的内容,如果您在试图帮助的人身上不断更改它。对不起……。我理解关于准备好的语句的部分,我只留下了一些代码,因为我尝试在没有bindParam和没有bindParam的情况下运行代码。抱歉,我会确保我的代码是下次发布时应该使用的代码,因为它很容易混淆抱歉…抱歉,我必须睡觉…一旦找到解决方案,我会发布代码。。。。。。。。。。。
$update = $db1->prepare("UPDATE users SET active=:active WHERE email=:email AND hash=:hash");