PHP--PHP Gets the IP address and host name

Source: Internet
Author: User

Client IP-related variables
1. $_server[' REMOTE_ADDR '; Client IP, which may be the IP of the user or the IP of the proxy.

2. $_server[' http_client_ip '; Proxy-side IP, may exist, can be forged.

3. $_server[' http_x_forwarded_for '; The user is the agent in which IP is used, which may exist and can be forged.

Server-side IP-related variables
1. $_server["SERVER_NAME"), need to use function gethostbyname () to obtain. This variable can be displayed correctly both on the server side and on the client.

2. $_server["Server_addr"], on the server side test: 127.0.0.1 (this is related to the Bindaddress setting value in httpd.conf). The test results on the client are correct.

Function

1, gethostbyaddr (IPAddress); //IP address to get the name of the host

2, gethostbyname (Strhostname); //Obtain an IP address by host name

3, getenv: Get environment variable content

Correlation function Getenv,setenv,unsetenv
Table header file #include <stdlib.h>
Defines the function getenv (const char *name);
The function Description getenv () is used to get the contents of the parameter name environment variable. The argument name is the name of the environment variable, and a pointer to that content is returned if the variable exists. The format of the environment variable is name=value.
Return value: Execution succeeds returns a pointer to that content, and returns null if no matching environment variable name is found.

4. Putenv: Change or increase environment variables

Correlation function Getenv,setenv,unsetenv
Table header file #include <stdlib.h>
Defines the function int putenv (const char * string);
Function Description: Putenv () is used to change or increase the contents of an environment variable. The format of the parameter string is Name=value, and if the environment variable originally existed, the contents of the variable would change depending on the parameter string, otherwise the content of the parameter would be the new environment variable.
Return value: 0 is returned when execution succeeds, and 1 if an error occurs.
Error code: ENOMEM There is not enough memory to configure the new environment variable space.

5. Setenv: Change or increase environment variables
Correlation function Getenv,putenv,unsetenv
Table header file #include <stdlib.h>
Defines the function int setenv (const char *name,const char * value,int overwrite);
The function Description setenv () is used to change or increase the contents of an environment variable.

The parameter name is a string of environment variable names.
The parameter value is the variable content.

The parameter overwrite is used to determine whether to change an existing environment variable. If the overwrite is not 0, the original content of the environment variable is changed, and the original content is changed to the variable content referred to in parameter value. If overwrite is 0 and the environment variable already has content, the parameter value is ignored.

A return value of 0 is returned when execution succeeds, and 1 is returned when an error occurs.
Error code ENOMEM Insufficient memory to configure new environment variable space

Host name of the current script server : gethostbyaddr ($_server[' remote_addr ')

Get the IP address of the server where the PHP script (page) is currently running: $_server[' server_addr '

Get the hostname of the server where the PHP script (page) is currently running: $_server[' server_name '

Client IP: $_server[' REMOTE_ADDR ']

Client ip:getenv (' REMOTE_ADDR ')

Server host Ip:gethostbyname ("www.qq.com")

PHP--PHP Gets the IP address and host name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.