dq13733519262 2016-04-21 15:25 采纳率: 0%
浏览 87

在wordpress上创建cookie后调用

Want to call a function after the logged_in cookie creation, but there is problem with my code.

I overrided the pluggable function wp_set_auth_cookie and added a hook after the cookie creation like this :

function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
//SOME INTERESTING CODE //
    /**
     * Fires immediately before the secure authentication cookie is set.
     *
     * @since 2.6.0
     *
     * @param string $logged_in_cookie The logged-in cookie.
     * @param int    $expire           Login grace period in seconds. Default 43,200 seconds, or 12 hours.
     * @param int    $expiration       Duration in seconds the authentication cookie should be valid.
     *                                 Default 1,209,600 seconds, or 14 days.
     * @param int    $user_id          User ID.
     * @param string $scheme           Authentication scheme. Default 'logged_in'.
     */
    do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in' );

    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);

    do_action( 'ic_logged_in_cookie_just_set');
}
endif;

As you can see, in the very end, i execute all function with the hook "ic_logged_in_cookie_just_set" so normally, after the cookie creation, it will execute my function.

But there is a problem : A function trigged by the hook who use the cookie don't have access to the cookie, so my distant app will receive an undefined cookie instead of the logged_in cookie.

My distant app is working, because when i use the 'init' to execute my action, it will work so fine.

My code :

class IC_Chat {

    public function __construct()
    {
        //add_action( 'init', array($this, 'ic_show_chat_bar'));
        add_action( 'ic_logged_in_cookie_just_set', array($this, 'ic_show_chat_bar'));
    }


    public function ic_show_chat_bar() {    
        //if(is_user_logged_in() && !is_admin()){
            $token = $this->askForToken();
        //} 
    }

    private function askForToken(){

        $url = 'http://10.1.1.232:4000/getToken';

        $cookieLoggedInName = "wordpress_logged_in_".md5( get_site_option("siteurl")) ;
        // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
            if (function_exists( 'hash' ) )
                $body = array("encryption" => "sha256");
            else 
                $body = array("encryption" => "sha1");

        $args = array(
            'user-agent' => 'Wordpress',
            'cookies' => array(
                $cookieLoggedInName =>$_COOKIE[$cookieLoggedInName]
            )
        );

        $resp = wp_remote_post( $url, $args );
        // $response_code = wp_remote_retrieve_response_message( $response );

        if ( is_wp_error($resp) ) {
            etc...
        }

    }   

}

My hook work too, because i receive a http post with it in my distant app log.

So how can i solve the problem, to execute my function when the cookie logged_in is create ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了