问题描述
我想检测是否使用PHP刷新了浏览器,如果刷新了浏览器,应该执行什么特定的PHP代码?
推荐答案
如果刷新了页面,那么您期望两个紧随其后的请求使用相同的URL(路径,文件名,查询字符串),并且请求相同表单内容(如果有)(POST数据).这可能是很多数据,因此最好对其进行哈希处理.所以…
<?php
session_start();
//The second parameter on print_r returns the result to a variable rather than displaying it
$RequestSignature = md5($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'].print_r($_POST, true));
if ($_SESSION['LastRequest'] == $RequestSignature)
{
echo 'This is a refresh.';
}
else
{
echo 'This is a new request.';
$_SESSION['LastRequest'] = $RequestSignature;
}