Home Backend Development PHP Tutorial 做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)_PHP

做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)_PHP

Jun 01, 2016 pm 12:07 PM
cdn

asp函数代码:
复制代码 代码如下:
function checkip(checkstring)'用正则判断IP是否合法
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()'取真实IP函数,先 HTTP_CLIENT_IP 再 HTTP_X_FORWARDED_FOR 再 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

discuz论坛取真实IP的php代码,其它类似,请参考稍做修改

(discuz修改include/common.inc.php)
用以下这段代码:
复制代码 代码如下:
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]=='192′ && $testip[1]=='168′) {
$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] =='192′ && $tmp_ip1[1] =='168′) {
$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);

}

替换这段代码:

复制代码 代码如下:
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'];
}

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which is the best registration-free CDN acceleration? Which is the best registration-free CDN acceleration? Jun 28, 2023 pm 04:42 PM

The best registration-free CDN acceleration is Cloudflare. Cloudflare provides global DDoS attack protection and web application security services, which can protect your website from malicious attacks.

How to configure and use CDN for acceleration in Vue How to configure and use CDN for acceleration in Vue Oct 15, 2023 pm 02:31 PM

How to configure and use CDN for acceleration in Vue In the Vue project, using CDN (ContentDeliveryNetwork) can effectively speed up web page loading and improve user experience. CDN technology distributes static resource files to servers in various locations around the world, allowing users to quickly obtain resources from the server closest to the user, reducing data transmission time and delays. The following will introduce in detail how to configure and use CDN for acceleration in Vue. First, we need to find a

Methods and applications of CDN acceleration using PHP Methods and applications of CDN acceleration using PHP Jun 18, 2023 pm 06:42 PM

With the rapid development of the Internet, website access speed has received more and more attention, and CDN (ContentDeliveryNetwork) is currently one of the most commonly used technologies to accelerate website access. CDN acceleration uses multiple servers distributed in different locations to forward user requests to servers closer to the user to speed up website access. PHP is a commonly used development language in implementing CDN acceleration. This article will introduce how to use PHP to implement CDN acceleration and apply it to actual

Baidu network disk crash problem has been solved and we apologize: It has been fixed. Please try to log in again. Baidu network disk crash problem has been solved and we apologize: It has been fixed. Please try to log in again. Aug 10, 2023 pm 02:25 PM

According to this website's report at 16:00 on August 9, Baidu's products have experienced large-scale failures. For example, Baidu Netdisk failed to play videos during use. Users of other businesses also reported that they could not be opened or crashed. After investigation by this site, it was found that the problem was caused by an abnormality in Baidu CDN service. Baidu Netdisk responded that it was very sorry for this problem and was urgently repairing it. Please wait patiently and thank you for your understanding. Five minutes later, Baidu Netdisk responded that the problem was Resolved, please try to log in again, thank you for your support. CDN (Content Distribution Network) distributes the origin site content to acceleration nodes around the world, so that users can be scheduled to the nearest acceleration node when requesting website resources, thus Get the content you need directly and improve users’ access to website resources

Optimize Python website access speed and use static resource acceleration methods such as CDN and browser caching. Optimize Python website access speed and use static resource acceleration methods such as CDN and browser caching. Aug 04, 2023 pm 05:09 PM

Optimize Python website access speed and use static resource acceleration methods such as CDN and browser caching. In today's Internet era, users have increasingly higher requirements for website access speed. A responsive website can provide a better user experience, thereby increasing user stickiness and satisfaction. In Python website development, by using static resource acceleration methods such as CDN (content delivery network) and browser caching, the access speed of the website can be significantly improved. CDN is a distributed server network that stores static resources away from users.

How to use Nginx reverse proxy and proxy_cache cache to build a CDN server How to use Nginx reverse proxy and proxy_cache cache to build a CDN server May 12, 2023 pm 05:43 PM

Encountered a problem: It is very slow for mobile users to access the web server www.osyunwei.com. Solution: 1. Place an nginx reverse proxy server in the mobile computer room. 2. Intelligent resolution through domain name dns. When all mobile users access www.osyunwei.com Resolve to nginx reverse proxy server 3. Dedicated line connection between nginx reverse proxy server and web server. Instructions: 1. Web server line: Telecom IP: 192.168.21.129 Domain name: www.osyunwei.com 2. nginx reverse proxy server line : Mobile system: centos6.2ip: 192.168.21.164vi/e

what is cdn what is cdn Aug 10, 2023 pm 04:28 PM

CDN is the abbreviation of Content Delivery Network, a distributed network architecture that distributes content to server nodes around the world to increase content transmission speed, reduce network latency, reduce server load, improve website reliability, and resist attacks. Architecture, through CDN, users can access the required content more quickly, which improves the user experience and also improves the performance and security of the website.

CDN in PHP CDN in PHP May 25, 2023 pm 07:31 PM

In recent years, with the continuous development of the Internet, the speed and stability of web pages have become important issues of concern to users. In response to this problem, CDN (ContentDeliveryNetwork, content distribution network) came into being. In PHP development, how to use CDN to improve the speed and stability of the website is a technology that developers need to pay attention to and apply. This article will introduce CDN technology in PHP, starting from understanding the basic principles of CDN to explaining in detail how to use CDN in PHP to improve web page loading.

See all articles