首页 > 解决方案 > 在 Wordpress/PHP 中保存全局令牌

问题描述

我正在使用Firebase JWT PHP 包生成 JWT 令牌。我到目前为止的代码如下所示:

function kh_generate_jwt () { 
    $jwt_key = get_field('jwt_key', 'option');
    $current_time = time();
    $minutes = 3;
    $expiration_time = strtotime('+ ' . $minutes . ' minutes', $current_time);
    $payload = array(
        "iss" => "XXX",
        "aud" => "XXX",
        "iat" => $current_time,
        "nbf" => $current_time,
        "exp" => $expiration_time,
        "uid" => get_current_user_id()
    );
    /**
     * IMPORTANT:
     * You must specify supported algorithms for your application. See
     * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
     * for a list of spec-compliant algorithms.
     */
    $jwt = JWT::encode($payload, $jwt_key);
    $decoded = JWT::decode($jwt, $jwt_key, array('HS256'));
}

然后应该可以在整个站点中将编码的有效负载$jwt用作某种全局变量。我听说全局变量不好用,所以我想知道生成这个令牌然后能够在不同情况下在整个页面上使用令牌的最佳解决方案是什么?

令牌的主要用途是将其作为参数附加到某些 iframe URL。因此,重要的是不要为每个 iframe 生成一个新令牌,而是在页面加载时拥有一个可以通过回显它来应用的全局令牌。

标签: phpwordpressjwt

解决方案


推荐阅读