简体   繁体   English

如何使用Apache或PHP转发客户端IP地址

[英]How To Forward Client IP Address Using Apache or PHP

Hello I was wondering is it possible to forward a client/visitor ip address to another site via redirect or curl. 您好,我想知道是否可以通过重定向或curl将客户端/访问者的IP地址转发到另一个站点。 For example lets say when a visitor comes to my site I want to do a redirect and send them to site1.com. 例如,假设当访问者访问我的网站时,我要进行重定向并将其发送到site1.com。 But instead of requesting that website using my ip address, I want to forward their ip address and use their very own ip address. 但是,我不想使用我的IP地址来请求该网站,而是要转发他们的IP地址并使用他们自己的IP地址。 Or for instance lets say I want to do a curl request, but instead of using my server's ip address, I want to forward their ip address and use their's. 例如,假设我要执行curl请求,但是我不想使用服务器的IP地址,而是要转发其IP地址并使用其IP地址。 Is this possible? 这可能吗? Or can anyone point me in the right direction?? 还是有人可以向我指出正确的方向? Thanks 谢谢

1) A Visitor Visits My Site
2) Visitor IP Address Gets Forwarded And Visitor Gets Redirected To Another 
         Site 
3) The Requesting Webpage See's Visitor IP Address Instead Of Mines
         Due to Forwarding

You cannot use someone else's IP address when making HTTP requests (say, via CURL or sockets) because it's your server that is doing the request, and IP addresses are detected by the remote server (you have no control over them). 发出HTTP请求时(例如,通过CURL或套接字),您不能使用其他人的IP地址,因为是您的服务器在执行请求,并且IP地址被远程服务器检测到(您无法控制它们)。

However, you can send a header that defines the user's IP address and that you're making the request on their behalf. 但是,您可以发送标头,该标头定义用户的IP地址,并代表用户提出请求。 X-Forwarded-For is commonly used for that. X-Forwarded-For通常用于此目的。 It is up to the remote server to honor this header. 由远程服务器决定是否接受此标头。

CURL example: CURL示例:

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-Forwarded-For: " . $_SERVER['REMOTE_ADDR']
]);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM