根据wordpress_logged_in_#cookie

时间:2019-05-19 14:06:12

标签: wordpress cookies

我需要一个cookie才能在缓存的页面上正确显示登录/匿名用户的动态链接。我以为默认的WordPress登录cookie有效期为14天,创建我的自定义14天有效脚本可访问cookie不会出错,它应该导致其有效期同步:

add_action('wp_login', 'lal_login');
function lal_login() {
    $expiration = time() + 14*24*60*60;
    setcookie('wp_user_logged_in', 1, $expiration, '/');
    $_COOKIE['wp_user_logged_in'] = 1;
}

add_action('wp_logout', 'lal_logout');
function lal_logout() {
    unset($_COOKIE['wp_user_logged_in']);
    setcookie('wp_user_logged_in', null, -1, '/');
}

不幸的是,Chrome浏览器显示2个默认cookie和我的自定义cookie的有效期不同。默认Cookie会在12小时后过期。

wordpress_logged_in _#

已创建 2019年5月19日星期日,下午3:01:54 过期 2019年6月3日,星期一,凌晨3:01:54

wordpress_sec _#

已创建 2019年5月19日星期日,下午3:01:54 过期 2019年6月3日,星期一,凌晨3:01:54

wp_user_logged_in

已创建 2019年5月19日星期日,下午3:01:54 过期 2019年6月2日,星期日,下午3:01:54

我发现WordPress增加了大约12小时的“宽限期”。

$expire = $expiration + ( 12 * HOUR_IN_SECONDS );

我应该简单地使Cookie在14天12小时后过期吗?与硬编码时间相比,我也更喜欢基于auth cookie(可基于过滤器进行更改)的到期时间。我该如何实现?下面的行显示了如何计算auth cookie过期时间:

$expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );

我看到WordPress也很喜欢设置为不记住用户,尽管我在理解它时遇到了问题,因为代码在下一行将$expire设置为0。我该如何支持字段以记住用户自定义Cookie中未正确选中的用户,以镜像WordPress身份验证Cookie?

位于pluggable.php中的负责设置身份验证cookie的整个函数如下:

function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
        if ( $remember ) {
            /**
             * Filters the duration of the authentication cookie expiration period.
             *
             * @since 2.8.0
             *
             * @param int  $length   Duration of the expiration period in seconds.
             * @param int  $user_id  User ID.
             * @param bool $remember Whether to remember the user login. Default false.
             */
            $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );

            /*
             * Ensure the browser will continue to send the cookie after the expiration time is reached.
             * Needed for the login grace period in wp_validate_auth_cookie().
             */
            $expire = $expiration + ( 12 * HOUR_IN_SECONDS );
        } else {
            /** This filter is documented in wp-includes/pluggable.php */
            $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );
            $expire     = 0;
        }

        if ( '' === $secure ) {
            $secure = is_ssl();
        }

        // Front-end cookie is secure when the auth cookie is secure and the site's home URL is forced HTTPS.
        $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );

        /**
         * Filters whether the connection is secure.
         *
         * @since 3.1.0
         *
         * @param bool $secure  Whether the connection is secure.
         * @param int  $user_id User ID.
         */
        $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );

        /**
         * Filters whether to use a secure cookie when logged-in.
         *
         * @since 3.1.0
         *
         * @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in.
         * @param int  $user_id                 User ID.
         * @param bool $secure                  Whether the connection is secure.
         */
        $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure );

        if ( $secure ) {
            $auth_cookie_name = SECURE_AUTH_COOKIE;
            $scheme           = 'secure_auth';
        } else {
            $auth_cookie_name = AUTH_COOKIE;
            $scheme           = 'auth';
        }

        if ( '' === $token ) {
            $manager = WP_Session_Tokens::get_instance( $user_id );
            $token   = $manager->create( $expiration );
        }

        $auth_cookie      = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
        $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );

        /**
         * Fires immediately before the authentication cookie is set.
         *
         * @since 2.5.0
         * @since 4.9.0 The `$token` parameter was added.
         *
         * @param string $auth_cookie Authentication cookie value.
         * @param int    $expire      The time the login grace period expires as a UNIX timestamp.
         *                            Default is 12 hours past the cookie's expiration time.
         * @param int    $expiration  The time when the authentication cookie expires as a UNIX timestamp.
         *                            Default is 14 days from now.
         * @param int    $user_id     User ID.
         * @param string $scheme      Authentication scheme. Values include 'auth' or 'secure_auth'.
         * @param string $token       User's session token to use for this cookie.
         */
        do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token );

        /**
         * Fires immediately before the logged-in authentication cookie is set.
         *
         * @since 2.6.0
         * @since 4.9.0 The `$token` parameter was added.
         *
         * @param string $logged_in_cookie The logged-in cookie value.
         * @param int    $expire           The time the login grace period expires as a UNIX timestamp.
         *                                 Default is 12 hours past the cookie's expiration time.
         * @param int    $expiration       The time when the logged-in authentication cookie expires as a UNIX timestamp.
         *                                 Default is 14 days from now.
         * @param int    $user_id          User ID.
         * @param string $scheme           Authentication scheme. Default 'logged_in'.
         * @param string $token            User's session token to use for this cookie.
         */
        do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token );

        /**
         * Allows preventing auth cookies from actually being sent to the client.
         *
         * @since 4.7.4
         *
         * @param bool $send Whether to send auth cookies to the client.
         */
        if ( ! apply_filters( 'send_auth_cookies', true ) ) {
            return;
        }

        setcookie( $auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
        setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
        setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
        if ( COOKIEPATH != SITECOOKIEPATH ) {
            setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为问题主要是cookie的到期时间,所以:

  

我看到WordPress也很荣幸设置为不记住用户,   尽管我在理解它时遇到问题,因为代码集$expire   到下一行的0。

当您调用setcookie()时,应将'expires'参数省略或将其设置为0,这两种方式都意味着Cookie将在会话结束时(浏览器关闭时终止) )。因此,这就是当用户在登录时未选中“记住我”复选框时WordPress需要将其设置为0的原因。

  

我该如何支持字段来记住用户未正确选中的用户   自定义Cookie,以反映WordPress身份验证Cookie?

您可以使用set_logged_in_cookie钩子来实际设置Cookie,而不是“支持它”。这样,无论用户是否选中“记住我”复选框,您都将获得预期的到期时间。

代码如下:

  1. 代替此:add_action('wp_login', 'lal_login');

    使用:add_action('set_logged_in_cookie', 'lal_login', 10, 2);

    或者这个add_action('set_auth_cookie', 'lal_login', 10, 2);

  2. 还有lal_login()

    function lal_login( $cookie, $expire ) {
        // If the user didn't check the "Remember Me" checkbox, $expire would be 0.
        // So if you want, you can set $expire to 14 days, 2 days, etc.
        //$expire = $expire ? $expire : time() + 14 * DAY_IN_SECONDS;
    
        setcookie('wp_user_logged_in', 1, $expire, '/');
        $_COOKIE['wp_user_logged_in'] = 1;
    }
    

    注意:如果钩接到set_logged_in_cookie,则$cookie将是已登录的cookie值;如果您钩住set_auth_cookie,则$cookie将是身份验证cookie值。