以下是表单代码:
以下是****.php中防止刷新的代码
//只能通过post方式访问
if ($_SERVER['REQUEST_METHOD'] == 'post')
{header('HTTP/1.1 404 Not Found'); die('亲,页面不存在');}
session_start();
$fs1=$_POST['aahide'];
$fs2=$_POST['bbhide'];
//防刷新时间,单位为秒
$allowTime = 30;
//读取访客ip,以便于针对ip限制刷新
/*获取真实ip开始*/
if ( ! function_exists('GetIP'))
{
function GetIP()
{
static $ip = NULL;
if ($ip !== NULL)
{
return $ip;
}
if (isset($_SERVER))
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
/* 取X-Forwarded-For中第x个非unknown的有效IP字符? */
foreach ($arr as $xip)
{
$xip = trim($xip);
if ($xip != 'unknown')
{
$ip = $xip;
break;
}
}
}
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
if (isset($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
else
{
$ip = '0.0.0.0';
}
}
}
else
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_CLIENT_IP'))
{
$ip = getenv('HTTP_CLIENT_IP');
}
else
{
$ip = getenv('REMOTE_ADDR');
}
}
preg_match("/[\d\.]{7,15}/", $ip, $onlineip);
$ip = ! empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return $ip;
}
}
/*获取真实ip结束*/
$reip = GetIP();
//相关参数md5加密
$allowT = md5($reip.$fs1.$fs2);
if(!isset($_SESSION[$allowT])){
$_SESSION[$allowT] = time();
}
else if(time() - $_SESSION[$allowT]-->$allowTime){
$_SESSION[$allowT] = time();
}
//如果刷新过快,则直接给出404header头以及提示
else {header('HTTP/1.1 404 Not Found'); die('来自'.$ip.'的亲,您刷新过快了');}
if ($_SERVER['REQUEST_METHOD'] == 'post')
{header('HTTP/1.1 404 Not Found'); die('亲,页面不存在');}
session_start();
$fs1=$_POST['aahide'];
$fs2=$_POST['bbhide'];
//防刷新时间,单位为秒
$allowTime = 30;
//读取访客ip,以便于针对ip限制刷新
/*获取真实ip开始*/
if ( ! function_exists('GetIP'))
{
function GetIP()
{
static $ip = NULL;
if ($ip !== NULL)
{
return $ip;
}
if (isset($_SERVER))
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
/* 取X-Forwarded-For中第x个非unknown的有效IP字符? */
foreach ($arr as $xip)
{
$xip = trim($xip);
if ($xip != 'unknown')
{
$ip = $xip;
break;
}
}
}
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
if (isset($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
else
{
$ip = '0.0.0.0';
}
}
}
else
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_CLIENT_IP'))
{
$ip = getenv('HTTP_CLIENT_IP');
}
else
{
$ip = getenv('REMOTE_ADDR');
}
}
preg_match("/[\d\.]{7,15}/", $ip, $onlineip);
$ip = ! empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return $ip;
}
}
/*获取真实ip结束*/
$reip = GetIP();
//相关参数md5加密
$allowT = md5($reip.$fs1.$fs2);
if(!isset($_SESSION[$allowT])){
$_SESSION[$allowT] = time();
}
else if(time() - $_SESSION[$allowT]-->$allowTime){
$_SESSION[$allowT] = time();
}
//如果刷新过快,则直接给出404header头以及提示
else {header('HTTP/1.1 404 Not Found'); die('来自'.$ip.'的亲,您刷新过快了');}
上述代码来自网络,经测试,好像不能获得ip地址,但能在一定时间内防止刷新。
打赏作者,期待美好
打赏鼓励作者,为网友提供更多更好的测试,也许一瓶水就够了。