php - 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']);
解决方案
$this->VerifyUserToken($GLOBALS['headers']['授权...
必须
$this->verifyUserToken($GLOBALS['headers']['授权
Windows 不区分大小写字母
问候,
亨里克
推荐阅读
- javascript - 日期格式的 Javascript Foreach 打印相同的日期
- bash - linux上读取文件的单字母命令
- c# - AdvancedDataGridView 在 ComboBoxColumn 过滤 c# WinForm 上崩溃
- c++ - Restrict namespace pollution in C++ Header
- javascript - 当有大量自定义标记(1k-10k)时,传单变慢
- postgresql - 获取分区表的表大小(Postgres 10+)
- python - 在 Python 中使用 SciPy 创建稀疏矩阵
- reactjs - Chrome API 的类型错误
- scala - 将列表过滤到 Scala 数据集中每个参数值的前 2 个案例类
- sharepoint - 使用 CSOM 在 Sharepoint 中创建文件夹并与没有 Microsoft 帐户的来宾用户共享