使用PHP检测浏览器是否刷新 [英] Detect whether the browser is refreshed or not using PHP

查看:172
本文介绍了使用PHP检测浏览器是否刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测是否使用PHP刷新了浏览器,如果刷新了浏览器,应该执行什么特定的PHP代码.

I want to detect whether the browser is refreshed or not using PHP, and if the browser is refreshed, what particular PHP code should execute.

推荐答案

如果刷新了页面,那么您期望两个紧随其后的请求使用相同的URL(路径,文件名,查询字符串),并且请求相同表单内容(如果有)(POST数据).这可能是很多数据,因此最好对其进行哈希处理.所以...

If the page was refreshed then you'd expect two requests following each other to be for the same URL (path, filename, query string), and the same form content (if any) (POST data). This could be quite a lot of data, so it may be best to hash it. So ...


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

在AJAX情况下,您必须注意将这段代码放入哪些文件中,以免为异步调用的脚本更新LastRequest签名.

In an AJAX situation you'd have to be careful about which files you put this code into so as not to update the LastRequest signature for scripts which were called asynchronously.

这篇关于使用PHP检测浏览器是否刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆