我一直在使用一种方式,禁止人们从网站通讯,当用户回复一封电子邮件时,使用“不受禁”这个词。
但我想改变这一点,所以人们只点击他们收到的电子邮件中的一个链接,重定向到我的网站,并成功地禁止该用户。但这种方式让我害怕人们可以玩网址,写不同的电子邮件和不被禁止的随机的人。
我知道这听起来很疯狂,但我对保安的事有点偏执。
我没有代码,因为我不知道如何实现这一正确的方式,意味着不玩url参数。
谢谢!
发布于 2018-03-04 04:05:34
用hmac()在电子邮件上签名。
$token = hash_hmac('sha256', 'email@example.com', 'secret-server-key');然后将其传递给电子邮件模板,这样您的取消订阅链接就会像:
<a href="http://example.com/unsubscribe?email=email@example.com&token=abcec5cdca4d760d34e8c02107ae68f251f08aaa9dd14956c2f0ab84a33f6441">然后,当用户单击它时,在电子邮件参数上签名,如果令牌匹配,则继续。
<?php
if (isset($_GET['email'], $_GET['token']) &&
$_GET['token'] === hash_hmac('sha256', $_GET['email'], 'secret-server-key')
) {
// lagit
}只要您不共享secret-server-key,这是安全的。
编辑:
接受@FunkFortyNiner的评论
您可以在电子邮件中签名并将其编码为单个令牌。本质上就像现在的JWT,但是没有json ;p
因此,您的令牌看起来应该是:ZW1haWxAZXhhbXBsZS5jb20.abcec5cdca4d760d34e8c02107ae68f251f08aaa9dd14956c2f0ab84a33f6441,但是在检查时会涉及更多的代码。如果你真的喜欢的话也许是值得的。
<?php
$email = 'email@example.com';
function base64url_encode($str) {
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
}
function base64url_decode($str) {
return base64_decode(strtr($str, '-_', '+/'));
}
$token = base64url_encode($email).'.'.hash_hmac('sha256', $email, 'secret-server-key');
// mock got token
$_GET['token'] = $token;
// when checking
if (isset($_GET['token'])) {
$tok = explode('.', $_GET['token']);
// check token parts
if (count($tok) !== 2) {
// not valid token
throw new \Exception('Invalid token!');
}
// check email segment
if (!isset($tok[0]) || !filter_var(base64url_decode($tok[0]), FILTER_VALIDATE_EMAIL)) {
// not valid email
throw new \Exception('Invalid token!');
}
$email = base64url_decode($tok[0]);
if ($tok[1] !== hash_hmac('sha256', $email, 'secret-server-key')) {
// failed verification
throw new \Exception('Invalid token!');
}
//
echo 'Its lagit!';
// do somthing with $email
}发布于 2018-04-01 16:34:12
我刚刚看到了如下内容:电子邮件中的链接-- unsubscribe.php --他的电子邮件中的用户密钥,在他的邮箱中确认了这一点。我知道,www.mywebtodo.net的时事通讯应用程序可能会更好一些。
https://stackoverflow.com/questions/49091772
复制相似问题