PHP:检测页面刷新 [英] PHP: Detect Page Refresh
问题描述
我有一个页面action.php
,我在该页面上通过代码运行SQL查询,因此,每当查看该页面时,查询的运行方式就会像计数页面浏览量
I have a page action.php
on which I run an SQL query through the code, so that whenever the page is viewed the query runs like its like counting page views
<?php
mysqli_query("UPDATE ****");
?>
问题是刷新页面时,查询会运行&页面刷新被计为我要避免的页面浏览.
The problem is when the page is refreshed, the query is run & PAGE REFRESH is counted as a PAGE VIEW which I want to avoid.
问题:如何避免这种情况?
Question: How to avoid it ?
我正在寻找一种简单的解决方案,以便我可以检查
What I am looking for is a simple solution so that I can check
if( page was refresh ) //some condition
{
do
}
推荐答案
我已经解决了这个问题……HURRAHHH没有会话&没有Cookie
i have solved the problem ... HURRAHHH with no session & no cookies
该解决方案是PHP:AJAX:JavaScript的组合
the solution is a combination of PHP : AJAX : JavaScript
您要在Page Load&上运行的查询不在页面上刷新通过AJAX调用运行,可以说我的功能是
the query that you want to run on Page Load & not on page Refresh run it as via AJAX call lets say my function for doing that is
function runQUERY()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","doIT.php",false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
}
通过执行以下操作,我可以简单地使用Javascript来检查页面是新加载还是刷新.
and i can simply check with Javascript that the page is a fresh load or its a refresh by doing the following
<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代码
and in your doIT.php simple add your PHP code which you were going to put in the normal page
<?php
mysql_query("UPDATE---------");
//put any code here, i won't run on any page refresh
?>
这篇关于PHP:检测页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!