我正在尝试检测我的网站页面的浏览器是否被刷新,以便在除Iphone之外的所有其他平台上执行一些logic.Apparently。任何帮助都是appreciated.Below,是我正在使用的php脚本。
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($pageWasRefreshed ) {
//do something because page was refreshed;
} else {
//do nothing;
}发布于 2020-07-04 09:24:29
看起来,Safari在刷新后不会发送Cache-Control头(这可能有几个原因)。这不是一个好主意,依靠浏览器通知您的刷新与头,因为浏览器可能会随着时间的推移。
解决此问题的一个更好的解决方案是使用会话,因为不管浏览器如何,它都能工作。
session_start(); //place it at the start of the PHP document, before anything except headers is send to browser
if (!isset($_SESSION["refresh"]))
$_SESSION["refresh"] = 0;
$_SESSION["refresh"] = $_SESSION["refresh"] + 1;
if ($_SESSION["refresh"] > 1)
{
//you refreshed the page!
}
else
{
//first time
}编辑:解决此问题的其他解决方案如下:
使用cookie的
。
https://stackoverflow.com/questions/62725710
复制相似问题