我有一个页面
action.php
,我在其中通过代码运行 SQL 查询,因此每当查看该页面时,查询都会像计算页面浏览量一样运行
<?php
mysqli_query("UPDATE ****");
?>
问题是刷新页面时,会运行查询并且页面刷新被计为我想避免的页面视图。
问题: 如何避免?
我正在寻找一个简单的解决方案,以便我可以检查
if( page was refresh ) //some condition
{
do
}
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($pageWasRefreshed ) {
//do something because page was refreshed;
} else {
//do nothing;
}
Ctrl+F5,F5,Ctrl+R,Enter)
$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";
}
if (isset($_COOKIE['action'])) {
// action already done
} else {
setcookie('action');
// run query
}
根据您的要求,您还需要决定何时删除 cookie 和/或再次执行操作。
<?php
session_start();
if (!$_SESSION['loaded'])
{
// insert query here
}
$_SESSION['loaded'] = true;
?>
解决方案是 PHP : AJAX : JavaScript 的组合
您想要在页面加载上运行而不是在页面刷新上运行的查询通过 AJAX 调用运行它,假设我执行此操作的函数是
function runQUERY()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","doIT.php",false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
}
我可以通过执行以下操作简单地使用 Javascript 检查该页面是新加载的还是刷新的
<head>
<script type="text/javascript">
function checkRefresh()
{
if( document.refreshForm.visited.value == "" )
{
// This is a fresh page load
alert ( 'Fresh Load' );
document.refreshForm.visited.value = "1";
..call you AJAX function here
}
else
{
// This is a page refresh
alert ( 'Page has been Refreshed, The AJAX call was not made');
}
}
</script>
</head>
<body onLoad="checkRefresh()">
<form name="refreshForm">
<input type="hidden" name="visited" value="" />
</form>
</body>
</html>
并在您的
doIT.php 中简单添加您要放入普通页面的 PHP 代码
<?php
mysql_query("UPDATE---------");
//put any code here, i won't run on any page refresh
?>
//here you get the url behind the domain.
$currentPage = $_SERVER['REQUEST_URI'];
//if the session current page is not set.
if(!isset($_SESSION['currentPage'])){
//set the session to the current page.
$_SESSION['currentPage'] = $currentPage;
}
//check if the session is not equal to the current page
if($_SESSION['currentPage'] != $currentPage){
// if it's not equal you set the session again to the current page.
$_SESSION['currentPage'] = $currentPage;
//set the query you want to use
}
if(isset($_GET["token"])) {
$view_token = $_GET["token"];
} else {
$new_views = $views + 1;
// UPDATE VIEWS
$view_token = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5);
header("Location: ?token=$view_token");
}
如果用户的 URL 中有令牌,则观看次数不会更新。因此,如果用户尝试刷新,则不会更新视图计数。当用户的 URL 中没有令牌时,视图计数会使用令牌更新并刷新页面。它跳出框框思考,但它有效!
添加表单字段,例如:“刷新”,除非是页面刷新,否则该值将始终增加。因为Page-Refresh只提交旧值。然后与 PHP 的 SESSION 变量中保存的相同值进行比较。如果是新页面,只需启动两个不同的值即可。
在 PHP 中:
if (isset($_POST['refresh']) && isset($_SESSION['refreshdetect']))
$pageWasRefreshed = ($_SESSION['refreshdetect'] == $_POST['refresh']);
$newstatus = $status;
if(!$pageWasRefreshed )
{
//.....change status
}
if (isset($_POST['refresh']))
{
$_SESSION['refreshdetect'] = $_POST['refresh'];
$row['refreshdetect'] = ((int)$_POST['refresh'])+1;
}
else //if new page
{
$_SESSION['refreshdetect'] = 0;
$row['refreshdetect'] = 1;
}
在 PHP->HTML 中(如果使用 Twig。更改为 PHP 代码以显示变量的值):
<input type="hidden" id="refresh" name="refresh" value="{{ row.refreshdetect }}" />
if( $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0')
{
$_SESSION['status'] = null;<br>
}