简体   繁体   English

我正在尝试将标头中的HTTP_X_FORWARDED_FOR传递给php服务器,但未在服务器中获取其值

[英]I'm trying to pass HTTP_X_FORWARDED_FOR in header to php server but not getting its value in server

I setup following php script on first server to print all server values 我在第一台服务器上设置以下php脚本以打印所有服务器值

var_dump($_SERVER)

from another server calling first server link 从另一个服务器调用第一个服务器链接

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => FIRST_SERVER_URL,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => array(
    "HTTP_X_FORWARDED_FOR: 111.90.148.19",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

But I'm not getting value of HTTP_X_FORWARDED_FOR in $_SERVER values 但是我没有在$ _SERVER值中得到HTTP_X_FORWARDED_FOR的值

I tried javascript like this. 我试过这样的javascript。

var data = null;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", FIRST_SERVER_URL);
xhr.setRequestHeader("HTTP_X_FORWARDED_FOR", "111.90.148.19");    
xhr.send(data);

You should set the header name in JS according to HTTP rules, not PHP internal name. 您应该根据HTTP规则在JS中设置标头名称,而不是PHP内部名称。 The header name is actually: 标头名称实际上是:

X-Forwarded-For

So your JS code should be like this: 因此,您的JS代码应如下所示:

...
xhr.setRequestHeader("X-Forwarded-For", "111.90.148.19");
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器无法识别 HTTP Header SOAPAction 的值 - Server did not recognize the value of HTTP Header SOAPAction 如何在 http 标头中将 JWT 从服务器传递到客户端 - How do I pass the JWT from the server to the client in a an http-header 无法读取/传递JS值到PHP服务器 - Not able to read/pass JS value to server in PHP 尝试运行Node http服务器时出现绑定错误 - Getting a binding error when trying to run Node http server 我正在尝试将对象键“品牌”及其值放入一个新数组中,但我遇到了麻烦 - I’m trying to get the object key “brand” and its value into a new array but I’m having trouble 消除“X-Powered-By”标头会自动消除HTTP响应中的“Server”标头 - Does eliminating “X-Powered-By” header automatically eliminate “Server” header from HTTP response 我正在尝试利用点击渠道,并将一些JS注入“标题自定义跟踪”中,但是在页面之后加载? - I'm trying to utilize Click Funnels, and inject into the “Header Custom Tracking” some JS, but its loading after the Page? 我正在尝试将 JavaScript 数据发送到我的 NodeJS 服务器 - I'm trying to send JavaScript data to my NodeJS server 尝试将客户端文本框中的值传递给服务器端方法 - Trying to pass a value in client side textbox to a server side method 我正在尝试在 javascript 中获取值以 JSON 格式在 PHP 中返回它 - I'm trying to get value in javascript to return it in PHP in JSON format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM