思路:
获取访问用户ip,查询数据库判断该ip是否可以继续注册新用户
示例
/*** Created by PhpStorm.
* User: Administrator
* Date: 2018/11/30
* Time: 19:35
* 限制一个ip一天只能注册10个账户
* 获取访问用户ip,查询数据库判断该ip是否可以继续注册新用户
*/
//获取数据库实例
$dsn = 'mysql:dbname=testhost=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上,欢迎点星网页链接
时间格式用int型,您做起来就会简单的多,你只要做在一天凌晨到24点这个时间内,注册的个数<5就可以了!/**客户端IP
*/
function getClientIp(){
if(getenv('HTTP_CLIENT_IP')) {
$onlineip = getenv('HTTP_CLIENT_IP')
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR')
} elseif(getenv('REMOTE_ADDR')) {
$onlineip = getenv('REMOTE_ADDR')
} else {
$onlineip = $_SERVER['REMOTE_ADDR']
}
return $onlineip
}//必须用这个,否则很多时候是取不到客户端的ip的$thisDayStart=mktime(0,0,0,date("m"),date("d"),date("Y"))//mktime($h,$i,$s,$m,$d,$y)$thisDayStart=mktime(23,59,59,date("m"),date("d"),date("Y"))//mktime($h,$i,$s,$m,$d,$y)select count(id) from com_users where addtime>=".$thisDayStart." and addtime<".$thisDayStart."只要这个count(id)>=5了,就不能可以了
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)