如何在注册后设置用户电子邮件验证?

时间:2018-06-30 作者:Philipp K

你知道所有这些网站是如何向新用户发送链接以供他们验证电子邮件地址的吗?我正试图建立这样的东西,但经过一些研究,我仍然没有找到一个关于如何实现这一点的好解释。

我对插件推荐持开放态度,但我发现的大多数插件都有很多其他我并不真正需要的功能。

如果不使用插件,我将如何将其添加到代码中?

我的方法是在注册后向用户元添加一个“Email not verified”属性,并向用户发送一封带有某种验证密钥的电子邮件。我如何验证用户是否确实单击了该链接?

谢谢你的建议

2 个回复
最合适的回答,由SO网友:Akshat 整理而成

您可以使用user_register

add_action( \'user_register\', \'my_registration\', 10, 2 );
function my_registration( $user_id ) {
    // get user data
    $user_info = get_userdata($user_id);
    // create md5 code to verify later
    $code = md5(time());
    // make it into a code to send it to user via email
    $string = array(\'id\'=>$user_id, \'code\'=>$code);
    // create the activation code and activation status
    update_user_meta($user_id, \'account_activated\', 0);
    update_user_meta($user_id, \'activation_code\', $code);
    // create the url
    $url = get_site_url(). \'/my-account/?act=\' .base64_encode( serialize($string));
    // basically we will edit here to make this nicer
    $html = \'Please click the following links <br/><br/> <a href="\'.$url.\'">\'.$url.\'</a>\';
    // send an email out to user
    wp_mail( $user_info->user_email, __(\'Email Subject\',\'text-domain\') , $html);
}
您可以检查$_GET[\'act\'] 然后通过更新元值激活有效密钥account_activated. 您可以使用wp_authenticate_user 每次用户尝试登录时,钩子都会验证激活状态。

要验证的代码段:

add_action( \'init\', \'verify_user_code\' );
function verify_user_code(){
    if(isset($_GET[\'act\'])){
        $data = unserialize(base64_decode($_GET[\'act\']));
        $code = get_user_meta($data[\'id\'], \'activation_code\', true);
        // verify whether the code given is the same as ours
        if($code == $data[\'code\']){
            // update the user meta
            update_user_meta($data[\'id\'], \'is_activated\', 1);
            wc_add_notice( __( \'<strong>Success:</strong> Your account has been activated! \', \'text-domain\' )  );
        }
    }
}

SO网友:MikeMoy

伙计们,我认为上述方法引入了不需要存在的安全漏洞。

电子邮件验证的主要目标是,我们希望注册的人提供他们拥有或至少有权访问的真实电子邮件地址。我们不希望人们注册其他人拥有的随机电子邮件地址,例如您的电子邮件地址。

上述代码存在漏洞,使黑客能够注册其他人拥有的随机电子邮件地址,然后在您的电子邮件验证页面上相对轻松地强制使用$user\\u id值和$code值。

1st vulnerability

您使用的是$user\\u id。现在我知道这个值可以是任意数,但通常是一个整数,特别是如果使用wordpress,这大约占互联网上网站的30%,并且查看上面的php代码,它确实是基于wordpress的。黑客要么在注册过程中获得$user\\u id,要么通过暴力猜出$user\\u id,从数字1开始,然后继续2、3、4、5、6。。。。。如果你的网站没有那么多的成员,他们会在不到一天甚至不到一个小时内猜出他们的$user\\u id。

2nd vulnerability

您正在使用MD5哈希函数和注册时间创建一个$code。黑客知道他们什么时候注册的。假设黑客下午3点注册。现在,黑客所要做的就是MD5哈希时间从下午2.55点到下午3.05点,他们将在不到一小时的时间内强制执行$代码。

Looking at the above the hacker can just brute force the $user_id and $code in less than a day and verify an email address that they do not own

啧啧啧啧

更好的方法是使用rand()函数生成$代码,使用大写(A-Z)和小写(A-Z)、数字(0-9)和特殊字符,例如(!&;#)。MD5哈希函数只使用数字0-9和小写字母a-f,而ye根据注册时间使用它的方式使其非常容易缩小范围和暴力攻击。

我已经编写了下面的PHP代码,生成了一个随机的$代码,其中包含大写字母/小写字母/整数/特殊字符。不要让黑客们这么容易。

function generateRandomString($stringLength){
    //specify characters to be used in generating random string, do not specify any characters that wordpress does not allow in the creation.
    $characters = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_[]{}!@$%^*().,>=-;|:?";

    //get the total length of specified characters to be used in generating random string
    $charactersLength = strlen($characters);

    //declare a string that we will use to create the random string 
    $randomString = \'\';

    for ($i = 0; $i < $stringLength; $i++) {
        //generate random characters
        $randomCharacter = $characters[rand(0, $charactersLength - 1)];
        //add the random characters to the random string
        $randomString .=  $randomCharacter;
    };

    //sanitize_user, just in case 
    $sanRandomString = sanitize_user($randomString);

    //check that random string contains Uppercase/Lowercase/Intergers/Special Char and that it is the correct length
    if ( (preg_match(\'([a-zA-Z].*[0-9]|[0-9].*[a-zA-Z].*[_\\W])\', $sanRandomString)==1) && (strlen($sanRandomString)==$stringLength) )
    {
        //return the random string if it meets the complexity criteria 
        return $sanRandomString;
    } else {
        // if the random string does not meet minimium criteria call function again 
        return call_user_func("generateRandomString",($stringLength) );
    }
}//end of generateRandomString function

//call the function to generate a random string with Uppercase Letters/Lowercase Letters/Intergers/Special Characters 
//into the function we pass the length of string we require, in this exampe it will generate a string 32 characters long
$code = generateRandomString(32);

echo $code;

结束

相关推荐

获取所有wp_USERS按元密钥排序

我正在尝试让用户列表按元键排序。我的意思是我已经设置了元键user_last_login. 某些用户没有此元密钥。一些用户拥有它。我想在一个查询中同时获得两个用户,但如果存在元键,则这些用户将排在第一位,其余用户应排在最后。这是我的问题$meta_query_args = array( \'relation\' => \'OR\', // Optional, defaults to \"AND\" array(