php根据IP以分钟限制提交次数

$limit变量设为一分钟可提交几次

例子可作为浏览人气,抽奖参考

<?php
header('Content-Type:text/html;charset=UTF-8');
session_start();
$limit = 5; //限制的次数
$ip = getip();
$time = date('Y-m-d H:i');
if (isset($_SESSION[$time])) {
    $ipnum = renum($_SESSION[$time], $ip);
} else {
    $ipnum = 0;
}
if ($ipnum >= $limit * 2) {
    exit("一分钟内只允许提交{$limit}次");
}
if (isset($_REQUEST['url'])) {
    $url = $_REQUEST['url'];
} else {
    $url = 0;
}
$_SESSION[$time][] = $ip;
exit("提交成功");
function getip()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
function renum($array, $get)
{
    $n = 0;
    foreach ($array as $val) {
        if ($val == $get) {
            $n++;
        }
    }
    return $n;
}

本文原创地址:https://www.edait.cn/article-919.html
版权所有 © 转载时必须以链接形式注明出处!

觉得本文对您有用,想收藏下来!方法很简单:请点击-〉

文章评论

暂无评论,来发表一个吧

发表 取消
ECSHOP插件网微信客服edait_cn