欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php中禁止单个IP与ip段访问的代码小结

程序员文章站 2023-02-26 16:46:59
禁止单个ip
禁止单个ip
<?php
//加ip访问限制
if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')) {
$userip = getenv('http_client_ip');
} elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')) {
$userip = getenv('http_x_forwarded_for');
} elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')) {
$userip = getenv('remote_addr');
} elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')) {
$userip = $_server['remote_addr'];
}
$banned_ip = array (
"127.0.0.1",
"203.0.0.1",
"56.12.50.65",
"192.168.1.88"
);
if (in_array($userip,$banned_ip))
{
die ("your ip is block to connect !");
}
echo "测试";
?>
禁止ip段
复制代码 代码如下:

<?php
//加ip访问限制
if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')) {
$userip = getenv('http_client_ip');
} elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')) {
$userip = getenv('http_x_forwarded_for');
} elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')) {
$userip = getenv('remote_addr');
} elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')) {
$userip = $_server['remote_addr'];
}
$ban_range_low=ip2long("217.0.0.0"); //ip段上
$ban_range_up=ip2long("217.255.255.255");//ip段尾
$ip=ip2long($userip]);
if ($ip>$ban_range_low && $ip<$ban_range_up)
{
print "banned";
exit();
}
?>