$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;
}