Php obtains the visitor IP address summary. Php get visitor IP address summary when very often we need to get the user's real IP address, such as, log records, geographic location, user information php get visitor IP address summary
Php obtains the visitor IP address summary
At most times, we need to get the real IP address of the user, for example, log record, geographic location, user information, website data analysis, etc, in fact, it is easy to get the IP address $ _ SERVER [\ 'remote _ ADDR. Next we will summarize several common methods for obtaining IP addresses.
// Method 1:
?
|
1
2 |
$ Ip = $ _ SERVER ["REMOTE_ADDR"];
Echo $ ip;
|
// Method 2:
The code is as follows:
$ User_IP = ($ _ SERVER ["HTTP_VIA"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]: $ _ SERVER ["REMOTE_ADDR"];
$ User_IP = ($ user_IP )? $ User_IP: $ _ SERVER ["REMOTE_ADDR"];
Echo $ user_IP;
// Method 3:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
Function getRealIp ()
{
$ Ip = false;
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Ip = $ _ SERVER ["HTTP_CLIENT_IP"];
}
If (! Empty ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Ips = explode (",", $ _ SERVER ['http _ X_FORWARDED_FOR ']);
If ($ ip) {array_unshift ($ ips, $ ip); $ ip = FALSE ;}
For ($ I = 0; $ I <count ($ ips); $ I ++ ){
If (! Eregi ("^ (10 │ 172.16 │ 192.168).", $ ips [$ I]) {
$ Ip = $ ips [$ I];
Break;
}
}
}
Return ($ ip? $ Ip: $ _ SERVER ['remote _ ADDR ']);
}
Echo getRealIp ();
|
// Method 4:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
If ($ HTTP_SERVER_VARS ["HTTP_X_FORWARDED_FOR"])
{
$ Ip = $ HTTP_SERVER_VARS ["HTTP_X_FORWARDED_FOR"];
}
Elseif ($ HTTP_SERVER_VARS ["HTTP_CLIENT_IP"])
{
$ Ip = $ HTTP_SERVER_VARS ["HTTP_CLIENT_IP"];
}
Elseif ($ HTTP_SERVER_VARS ["REMOTE_ADDR"])
{
$ Ip = $ HTTP_SERVER_VARS ["REMOTE_ADDR"];
}
Elseif (getenv ("HTTP_X_FORWARDED_FOR "))
{
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
}
Elseif (getenv ("HTTP_CLIENT_IP "))
{
$ Ip = getenv ("HTTP_CLIENT_IP ");
}
Elseif (getenv ("REMOTE_ADDR "))
{
$ Ip = getenv ("REMOTE_ADDR ");
}
Else
{
$ Ip = "Unknown ";
}
Echo $ ip;
|
// Method 5:
?
|
1
2
3
4
5
6
7
8
9
10 |
If (getenv ('http _ CLIENT_IP ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Else {
$ Onlineip = $ HTTP_SERVER_VARS ['remote _ ADDR '];
}
Echo $ onlineip;
|
// Method 6:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
Print "your IP address is :";
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Cip = $ _ SERVER ["HTTP_CLIENT_IP"];
}
Elseif (! Empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {
$ Cip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
}
Elseif (! Empty ($ _ SERVER ["REMOTE_ADDR"]) {
$ Cip = $ _ SERVER ["REMOTE_ADDR"];
}
Else {
$ Cip = "cannot be obtained! ";
}
Print $ cip;
|
The above is all the content of this article. I hope it will help you learn php.
Accessing php to obtain visitors' IP addresses. in this case, we need to obtain the real IP addresses of users, such as log records, geographic location, and user information...