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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP