Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does PHP get the real IP of visitors?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to get the real IP of visitors by PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to get the real IP of visitors by PHP"!

In the absence of a proxy server, directly use:

$ip = $_ SERVER ["REMOTE_ADDR"]

If the client uses a proxy server to access, it gets the IP address of the proxy server, not the real client IP address, and vice versa, the real IP

Here is a general way to write it, and you can get it all:

To get the real IP address of the client through the proxy server, use getenv ("HTTP_X_FORWARDED_FOR") to read it.

But if the client is not accessed through the proxy server, the value obtained with getenv ("HTTP_X_FORWARDED_FOR") will be empty.

Function getIP () {static $realip; if (isset ($_ SERVER)) {if (isset ($_ SERVER ["HTTP_X_FORWARDED_FOR"])) {$realip = $_ SERVER ["HTTP_X_FORWARDED_FOR"];} else if (isset ($_ SERVER ["HTTP_CLIENT_IP"])) {$realip = $_ SERVER ["HTTP_CLIENT_IP"];} else {$realip = $_ SERVER ["REMOTE_ADDR"] }} else {if (getenv ("HTTP_X_FORWARDED_FOR")) {$realip = getenv ("HTTP_X_FORWARDED_FOR");} else if (getenv ("HTTP_CLIENT_IP")) {$realip = getenv ("HTTP_CLIENT_IP");} else {$realip = getenv ("REMOTE_ADDR");}} return $realip;}

Note:

The difference between $_ SERVER and getenv. Getenv does not support php running in IIS's isapi mode.

The getenv ("REMOTE_ADDR") function can obtain the ip address normally under apache, but it has no function in iis. The $_ SERVER ['REMOTE_ADDR'] function, which can successfully obtain the ip address of the visitor in apache, is also valid in iis.

In addition, according to the ip address, we can implement a similar anti-brushing mechanism that restricts IP access.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report