外贸网站必备:为ecshop加上IP屏蔽功能,限制ip访问
前几天做外贸站的时候遇到个问题,就是如何限制IP地址段访问,ecshop默认是没有这个功能的,到网上搜了一下,凑了一些代码,自己再加工,添加了一些代码,有了以下成品,先看效果:
20110928修复可耻的IE6兼容问题
后台界面如下:
设定屏蔽IP段后,前台效果如下:
接下来,寒风讲讲具体实现方法(安全起见,仅屏蔽前台,屏蔽IP段地址可在后台设置!):
前台
1. includes文件夹下新建ip.php文件,内容为:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php
/*///////////////////////////////////
//屏蔽IP地址段 V0.1
//时间:2011.09.24 15:40
//代码:http://www.hisune.com
*/////////////////////////////////////
class IP{ //获取客户IP地址
function getIpAdr(&$ip){
$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"];
}
}
function get_netip($myip){ //只留客户IP地址的前三位
$temp=explode(".",$myip);
$netip.=$temp[0];
$netip.=".";
$netip.=$temp[1];
$netip.=".";
$netip.=$temp[2];
return $netip;
}
$filename="includes\ip.txt"; //定义操作文件
$ip_lib=file($filename); //读取文件数据到数组中
$allow=0;
$IP=new IP;
$thisip="";
$IP->getIpAdr($thisip);
$thenetip=get_netip($thisip);
for($i=0;$i<count($ip_lib);$i++){
if(ereg($thenetip,$ip_lib[$i])){
$allow=1;
break;
}
}
if ($allow==1)
{
echo "<script>location.href='error.php';</script>";
}
//else {
// echo "";
//}
/*///////////////////////////////////
//屏蔽IP地址段 V0.1
//时间:2011.09.24 15:40
//代码:http://www.hisune.com
*/////////////////////////////////////
?> |
2. includes文件夹下新建ip.txt空白文本文档
3. 根目录下新建error.php,内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>错误,您的IP不在本站的服务范围之内。</title>
</head>
<body>
<div style=" margin:80px 200px; border: solid 1px #C0C0C0;">
<div style=" border: solid 3px #EAEAEA; padding:30px; text-align:center">
<img src="themes/bk/images/warning.gif" /> 抱歉,您的IP地址不在本站的服务范围之内!请联系网站管理员。<br />
Sorry, your IP address is not within the scope of the services this site! Please contact the webmaster.
</div>
</div>
</body>
</html> |
4 . 在includes文件夹下init.php文件末端插入:
1 | require(ROOT_PATH . 'includes/ip.php'); |
后台
5. admin文件夹下新建ip.php文件,内容为:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php
/**
* IP地址段屏蔽后台程序
* ============================================================================
* 版权所有 2011-2012 海尚网络,并保留所有权利。
* 网站地址: http://www.hisune.com;
* ----------------------------------------------------------------------------
*文件版本:0.1
*编写时间:2011-09-22 13:55:12
*作者:寒风
*个人网站:http://www.coolwinds.cn
*专业建站:http://www.hisune.com
* ============================================================================
**/
//read
$fp = fopen("../includes/ip.txt", 'r');
flock($fp, LOCK_SH);
$content = fread($fp, filesize("../includes/ip.txt"));
flock($fp, LOCK_UN);
fclose($fp);
//display
echo "<div style=\" border:solid 1px #D6D6D6; background:#EAEAEA; margin-left:15px; margin-right:15px;height:35px; line-height:35px; font-weight:bold; font-size:14px; text-align:center;\">IP段屏蔽设置</div>";
echo "<div style=\" border-left:solid 1px #D6D6D6;border-right:solid 1px #D6D6D6;border-bottom:solid 1px #D6D6D6; overflow:auto; zoom:1; padding-top:10px; margin-left:15px; margin-right:15px;\">";
echo "<div style=\"float:left; margin-left:10px; _margin-left:5px; \">";
echo "<form method=\"post\" action=\"ip.php\">";
echo "<textarea name=\"ipkill\" style=\" height:200px; width250px; font-size:12px;\" name=\"ipkill\" id=\"ipkill\">$content</textarea>";
echo "<br />";
echo "<br />";
echo "<input type=\"submit\" name=\"submit\" value=\"保存\" style=\" cursor:pointer;\"> ";
echo "<input type=\"reset\" name=\"reset\" value=\"重置\" style=\" cursor:pointer;\">";
echo "</form>";
echo "</div>";
echo "<div style=\"float:right; font-size:12px; margin-right:10px;_margin-right:5px; border:dashed 1px #D6D6D6; padding:5px; \">";
echo "<div style=\"width:98%; padding:3px; background:#EBEBEB;\">1. 在左侧输入要屏蔽的IP地址段</div>";
echo "<br />";
echo "<div style=\"width:98%; padding:3px; background:#EBEBEB;\">2. IP地址段为前三位,每一行一个IP段如:</div>";
echo "192.168.1";
echo "<br />";
echo "192.168.0";
echo "<br />";
echo "<br />";
echo "<div style=\"width:98%; padding:3px; background:#EBEBEB;\">3. 您当前登录的IP地址为:</div>";
echo "<font color=\"#ff0000;\">".$ip=$_SERVER["REMOTE_ADDR"]."</font>";
echo "<br />";
echo "</div>";
echo "</div>";
//write
if($_POST['submit']){
$ft = fopen("../includes/ip.txt","w+");
fwrite($ft,"$_POST[ipkill]");
fclose($ft);
echo "<script language=\"javascript\">alert('保存成功!');history.go(-1)</script>";
}
/**
* IP地址段屏蔽后台程序
* ============================================================================
* 版权所有 2011-2012 海尚网络,并保留所有权利。
* 网站地址: http://www.hisune.com;
* ----------------------------------------------------------------------------
*文件版本:0.1
*编写时间:2011-09-22 13:55:12
*作者:寒风
*个人网站:http://www.coolwinds.cn
*专业建站:http://www.hisune.com
* ============================================================================
**/
?> |
6. admin/templates/top.htm文件:
搜索:
1 | <li><a href="{$key}" target="main-frame">{$item}</a></li> |
在其后加入:
1 | <li><a href="javascript:window.open('ip.php','_blank','width=620,height=350');void(0)">IP段屏蔽</a></li> |
这样就可以在ecshop后台导航条处看到链接了
所有代码如上,寒风自己已经测试成功了。
当然,以上方法不仅限于添加到ecshop,添加到任意php站点都是可以的!寒风所写的以上内容只是以ecshop为例!
最后,再次AD一下,我的新站正在准备中,嘿嘿~
声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: 外贸网站必备:为ecshop加上IP屏蔽功能,限制ip访问



你的站是怎么了啊?今天这速度怎么这么不给力啊。。。死慢死慢的
@懒人梦醒
…不清楚,美国主机就这样
php高手。。膜拜
小功能,大用处
这个程序能限制蜘蛛吗?
@weyoga
蜘蛛用robots.txt
User-agent: *
Disallow: /
寒风:后台导航条里面会出现4个IP段屏蔽设置,要什么解决?
@浪子
你确定第六步无错?