PHP:检测页面刷新

2022-08-30 19:25:26

我有一个页面,我通过代码运行SQL查询,因此每当查看页面时,查询的运行方式就像计算页面视图一样action.php

<?php
mysqli_query("UPDATE ****");
?>

问题是当页面刷新时,查询被运行,PAGE REFRESH被计为我想要避免的页面视图。

问题:如何避免?

我正在寻找的是一个简单的解决方案,以便我可以检查

if( page was refresh ) //some condition
{
 do
}

答案 1

我在这里找到了这个片段,它非常适合我:

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

if($pageWasRefreshed ) {
   //do something because page was refreshed;
} else {
   //do nothing;
}

答案 2

检测页面刷新的最佳方法。还是不是?(+,,+,CtrlF5F5CtrlREnter)

$pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) &&($_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' ||  $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'); 
if($pageRefreshed == 1){
    echo "Yes page Refreshed";
}else{
    //enter code here
    echo "No";
}

推荐