首页 > 解决方案 > php 超全局函数 ($GLOBALS['headers']['Authorization']) 从未设置。为什么?

问题描述

在此处输入图像描述我有 php 函数应该验证是否有令牌,如果有,请在我的表中搜索匹配的公司名称。当我在 localhost 中使用 xampp 时,该功能工作正常。当我在服务器上的 prod 中执行此操作时,它给了我一个“令牌未定义”错误,什么可能导致该错误?我的php函数

    public function findCompany(){
        echo "global var= ";
//        var_dump($GLOBALS['headers']['Authorization']);
//        var_dump($GLOBALS);
        var_dump($_SERVER['REMOTE_ADDR']);

        if (isset($GLOBALS['headers']['Authorization'])) {
            if ($id = $this->VerifyUserToken($GLOBALS['headers']['Authorization'], $_SERVER['REMOTE_ADDR'])) {
        $data = [
            'company_name' => $_POST['company_name']
        ];

        $companies = $this->currentModel->findCompany($data);

        if($companies){
            echo json_encode($companies);
        } else {
            echo json_encode(['success' => false]);
        }
    }
            else {
                echo json_encode(['success' => false, 'error' => "invalid token"]);
            }

        } else {
            echo json_encode(['success' => false, 'error' => "token undefined"]);

        }

    }
}

verifyUserToken 函数

public function verifyUserToken($token, $ip) {
        $db = new Database();

        $db->query('SELECT * FROM auth WHERE token = :token AND expiry >now()');

        $db->bind(':token', $token);
        //check database if token exists and is not expired
        if($res = $db->single()) {
            // checks if token matches to ip address
            // returns user or contact id if verified else returns false
            if($res->token === $token && $res->ip === $ip) {
                $this->cleanTokens();
                if($res->user_id >0) {
                    return $res->user_id;
                }
//              
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

我检查了数据库,令牌显然在那里。无论如何,它不会给我一个无效的令牌消息。看起来令牌没有被发送。当我在我的机器上的 localhost 中执行此操作时,它工作正常。

为了调试,我使用 var_dump 查看发送的内容。我对 PHP 没有太多经验,但看起来我的标头授权从未设置过。有什么解决办法

请参阅下面的图像和代码,了解我在尝试时在控制台中得到的内容。出于安全目的,我截断了一些路径

        echo "global var= ";
and
        var_dump($GLOBALS);
you get
   ["GLOBALS"]=>
    *RECURSION*
    ["headers"]=>
    array(13) {
      ["Host"]=>
      string(20) "globalplantbased.com"
      ["Connection"]=>
      string(10) "keep-alive"
      ["Content-Length"]=>
      string(2) "15"
      ["Pragma"]=>
      string(8) "no-cache"
      ["Cache-Control"]=>
      string(8) "no-cache"
      ["Accept"]=>
      string(33) "application/json, text/plain, */*"
      ["User-Agent"]=>
      string(115) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
      ["Content-Type"]=>
      string(33) "application/x-www-form-urlencoded"
      ["Origin"]=>
      string(27) "http://globalplantbased.com"
      ["Referer"]=>
      string(59) "http://globalplantbased.com"
      ["Accept-Encoding"]=>
      string(13) "gzip, deflate"
      ["Accept-Language"]=>
      string(14) "en-US,en;q=0.9"
      ["Cookie"]=>
      string(31) "_ga=GA1.2.1321601484.1609694939"

或 echo "global var="; 和 var_dump($GLOBALS['headers']['Authorization']);

或 echo "global var="; 和 var_dump($_SERVER['REMOTE_ADDR']);

[![($GLOBALS['headers']['Authorization']) 2 ] 2 ($_SERVER['REMOTE_ADDR'])

标签: phpoauth-2.0authorizationtokensuperglobals

解决方案


$this->VerifyUserToken($GLOBALS['headers']['授权...

必须

$this->verifyUserToken($GLOBALS['headers']['授权

Windows 不区分大小写字母

问候,

亨里克


推荐阅读