很多站长朋友们都不太清楚php限制ip,今天小编就来给大家整理php限制ip,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 如何用php实现IP访问限制 2、 如何用php实现IP限制 3、 PHP中限制IP段访问、禁止IP提交表单的代码 4、 如何在php上限制一个ip一天只能注册10个账户,注册多了不允许 5、 php怎么限制某个ip或ip段过多的请求 6、 求一段PHP限制IP及IP段访问的代码,拜托了。。 如何用php实现IP访问限制function nTabs(thisObj,Num){
if(thisObj.className == "active")return;
var tabObj = thisObj.parentNode.id;
var tabList = document.getElementById(tabObj).getElementsByTagName("li");
for(i=0; i <tabList.length; i++)
{
如何用php实现IP限制<?php
error_reporting(7);
session_start();
// 发送字符头信息
if ($headercharset)
header("Content-Type:text/html; charset=gb2312");
// 加载公共文件
require_once("config.php");
require_once("global.php");
require_once("db_mysql.php");
/***************** 进行客户端能否访问本网站校验 ************/
// 获取客户端IP
if(getenv('HTTP_CLIENT_IP')) {
$client_ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$client_ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR')) {
$client_ip = getenv('REMOTE_ADDR');
} else {
$client_ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
// 分解客户端IP
$cip = explode(".", $client_ip);
// 连接数据库
$db = new DB_Sql();
$err = $db->connect();
/* 限制远程IP访问, PS: 这段代码真晕,呵呵,用了8个if, -_-# */
// 从数据库中提取存储的要限制的IP地址
$query_str = "SELECT limit_ip FROM us_limitip";
$db->query($query_str);
// 把结果循环提取,一个个进行校验
while ($db->next_record())
{
$limit_ip = $db->f("limit_ip");
$lip = explode(".", $limit_ip);
// 如果限制IP的第一个是*或者是0的话就跳到错误页
if (($lip[0]=='*') || ($lip[0]=='0'))
header("Location:error.php?errid=300");
// 如果刚好客户端IP等于我们限制IP就跳到错误页
if ($client_ip==$limit_ip)
header("Location:error.php?errid=300");
// 如果第一组IP一致进行第二组IP的匹配
if ($cip[0] == $lip[0])
{
// 如果第二组限制IP是*就跳到错误页
if ($lip[1]=='*')
header("Location:error.php?errid=300");
// 第二组IP匹配就进行第三组IP匹配
if ($cip[1]==$lip[1])
{
// 如果第三组限制字符是*就跳到错误页
if ($lip[2]=='*')
header("Location:error.php?errid=300");
// 如果第三组IP匹配就跳到第三组校验
if ($cip[2]==$lip[2])
{
// 如果第四组限制IP是*或0就跳到错误页
if (($lip[3]=='*') || ($lip[3]=='0'))
header("Location:error.php?errid=300");
}
}
}
}
// 释放数据库查询结果
$db->free();
/****************** IP校验结束 ******************/
?>
PHP中限制IP段访问、禁止IP提交表单的代码我们只要在feedback.php中添加下面的代码进行判断就可以了。
注意:下边只是一个PHP限制IP的实例代码,如果您打算应用到CMS中,请自行修改,或者如果您正在使用DEDECMS,可以联系本站。
复制代码
代码如下:
<?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'];
}
//限制ip
if
($userip=='27.37.188.128'){
header("location:");//被禁止后跳转到脚本之家站
exit;
}
//限制ip段
$ip_arr
=
explode('.',
$userip);
#限制的ip段,假设是192.168.*.*
if
(!(($ip_arr[0]
==
'192'
$ip_arr[1]=='168')
)){
header("location:");//被禁止后跳转到脚本之家素材站
exit;
}else{
header("location:");//正常IP则直接访问脚本之家首页
exit;
}
?>
如何在php上限制一个ip一天只能注册10个账户,注册多了不允许思路:
获取访问用户ip,查询数据库判断该ip是否可以继续注册新用户
示例
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/11/30
* Time: 19:35
* 限制一个ip一天只能注册10个账户
* 获取访问用户ip,查询数据库判断该ip是否可以继续注册新用户
*/
//获取数据库实例
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$db = new PDO($dsn, $user, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
//获取访问用户ip
$access_user_ip = $_SERVER['REMOTE_ADDR'];
//查询数据库判断该ip是否可以继续注册新用户
$start_time = strtotime(date('Y-m-d'));//今天0点
$end_time = strtotime(date('Y-m-d').' +1 day ');//明天0点
$sth = $db->prepare('select count(*) from user where ip=:ip and created_at>:start_time and created_at<:end_time');
$sth->bindParam(':ip',$access_user_ip);
$sth->bindParam(':start_time',$start_time);
$sth->bindParam(':end_time',$end_time);
$sth->execute();
$count = $sth->fetchColumn();//当前该ip今天注册的用户总数量
if ($count>10){
exit('今天,您已注册10个新账号了,请明天再来吧');
}
源码放在github上,欢迎点星网页链接
php怎么限制某个ip或ip段过多的请求//禁用单个ip如下:<?php //禁用ip地址 $ip=$_SERVER["REMOTE_ADDR"]; $ban=file_get_contents("ban.dat"); if(stripos($ban,$ip)) { die("Your IP Address is:$ip,you're forbiden to view this page!"); } echo "Your IP Address is:$ip,hello!"; ?>//禁用ip段如下:<?php //禁用ip地址 $ip=$_SERVER["REMOTE_ADDR"]; while($ip[count($ip-1)]!='.')$ip=substr($ip,1, -1); //整理出ip段 $ban=file_get_contents("ban.dat"); if(stripos($ban,$ip)) { die("U're forbiden to view this page!"); } echo "Hello!"; ?>
求一段PHP限制IP及IP段访问的代码,拜托了。。正确代码
function check_ip(){
$ALLOWED_IP=array('192.168.2.*','127.0.0.1','192.168.2.49');
$IP=getIP();
$check_ip_arr= explode('.',$IP);//要检测的ip拆分成数组
#限制IP
if(!in_array($IP,$ALLOWED_IP)) {
foreach ($ALLOWED_IP as $val){
if(strpos($val,'*')!==false){//发现有*号替代符
$arr=array();//
$arr=explode('.', $val);
$bl=true;//用于记录循环检测中是否有匹配成功的
for($i=0;$i<4;$i++){
if($arr[$i]!='*'){//不等于* 就要进来检测,如果为*符号替代符就不检查
if($arr[$i]!=$check_ip_arr[$i]){
$bl=false;
break;//终止检查本个ip 继续检查下一个ip
关于php限制ip的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php限制ip php限制ip访问网站的详细内容...