PHP implements encapsulation class to obtain client and server IP

墨辰丷
Release: 2023-03-29 09:50:01
Original
2229 people have browsed it

This article mainly introduces the encapsulation class for PHP to obtain client and server IP. It briefly analyzes the basic usage of PHP using server predefined variables and performs a simple encapsulation. Friends who need it can refer to it

The details are as follows:

Client IP related variables:

1. $_SERVER['REMOTE_ADDR']; Client IP, which may be the user’s IP, It may also be the proxy IP.

2. $_SERVER['HTTP_CLIENT_IP']; The IP of the agent may exist and can be forged.

3. $_SERVER['HTTP_X_FORWARDED_FOR']; Which IP the user uses as a proxy may exist and can be forged.

Server-side IP related variables:

1. $_SERVER["SERVER_NAME"] needs to be obtained using the function gethostbyname(). This variable displays correctly on both the server and client sides.

2. $_SERVER["SERVER_ADDR"], tested on the server side: 127.0.0.1 (this is related to the setting value of BindAddress in httpd.conf). The test results on the client are correct.

The categories are as follows:

class getIP{
 function clientIP(){
 $cIP = getenv('REMOTE_ADDR');
 $cIP1 = getenv('HTTP_X_FORWARDED_FOR');
 $cIP2 = getenv('HTTP_CLIENT_IP');
 $cIP1 ? $cIP = $cIP1 : null;
 $cIP2 ? $cIP = $cIP2 : null;
 return $cIP;
 }
 function serverIP(){
 return gethostbyname($_SERVER["SERVER_NAME"]);
 }
}
$getIP = new getIP();
$clientIp = getIP::clientIP();
$serverIp = getIP::serverIP();
echo &#39;Client IP is &#39;,$clientIp,&#39;<br />&#39;;
echo &#39;Server IP is &#39;,$serverIp,&#39;<br />&#39;;
Copy after login

##Summary: The above is the entire content of this article, I hope it can be helpful to everyone Learning helps.

Related recommendations:

Examples of PHP conforming to PSR programming specifications

php curl simulated login And get the detailed explanation of the 4 common running methods of the data instance

##php

##

The above is the detailed content of PHP implements encapsulation class to obtain client and server IP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template