Function code for obtaining users' real IP addresses through CDN (PHP and Asp setting method) _ PHP Tutorial

Source: Internet
Author: User
Implemented the function code for CDN to obtain the real IP address of the user (PHP and Asp settings ). Asp function code: The copy code is as follows: functioncheckip (checkstring) uses regular expressions to determine whether the IP address is valid dimre1setre1newRegExpre1. pattern "^ [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 asp function code:

The code is as follows:


Function checkip (checkstring) 'uses regular expressions to determine whether the IP address is valid.
Dim re1
Set re1 = new RegExp
Re1.pattern = "^ [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3} $"
Re1.global = false
Re1.Ignorecase = false
Checkip = re1.test (checkstring)
Set re1 = nothing
End function

Function get_cli_ip () ': obtains the real IP address. first, HTTP_CLIENT_IP, then HTTP_X_FORWARDED_FOR, and then REMOTE_ADDR.
Dim client_ip
If checkip (Request. ServerVariables ("HTTP_CLIENT_IP") = true then
Get_cli_ip = checkip (Request. ServerVariables ("HTTP_CLIENT_IP "))
Else
MyArray = split (Request. ServerVariables ("HTTP_X_FORWARDED_FOR "),",")
If ubound (MyArray)> = 0 then
Client_ip = trim (MyArray (0 ))
If checkip (client_ip) = true then
Get_cli_ip = client_ip
Exit function
End if
End if
Get_cli_ip = Request. ServerVariables ("REMOTE_ADDR ")
End if
End function

Use the php code of the real IP address in the discuz Forum. for other similar code, see make some modifications.

(Discuz modify include/common. inc. php)
Use the following code:

The code is as follows:


If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR '),

'Unknown ')){

$ Testip = explode ('.', getenv ('http _ X_FORWARDED_FOR '));

If ($ testip [0] = '000000' & $ testip [1] = '000000 ′){
$ Onlineip = getenv ('remote _ ADDR ');
}
Elseif ($ testip [0] = '10 ′){
$ Onlineip = getenv ('remote _ ADDR ');
}
Else {
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
}

// Gamesir hack end} elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR '),

'Unknown ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR'] & strcasecmp ($ _ SERVER

['Remote _ ADDR '], 'Unknown ')){
// By Johnny
$ Tmp_ip = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']);
$ Tmp_ip1 = explode (',', $ tmp_ip [0]);
If ($ tmp_ip1 [0] = '000000' & $ tmp_ip1 [1] = '000000 ′){
$ Onlineip = getenv ('remote _ ADDR ');
} Else if ($ tmp_ip1 [0] = '10 ′){
$ Onlineip = getenv ('remote _ ADDR ');
}
Else {
$ Onlineip = $ tmp_ip [0];
}
Unset ($ tmp_ip); unset ($ tmp_ip1 );

}

Replace this code:

The code is as follows:


If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR '),

'Unknown ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR'] & strcasecmp ($ _ SERVER

['Remote _ ADDR '], 'Unknown ')){
$ Onlineip = $ _ SERVER ['remote _ ADDR '];
}

The authorization code is as follows: function checkip (checkstring) 'uses regular expressions to determine whether the IP address is valid. dim re1 set re1 = new RegExp re1.pattern = "^ [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3...

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.