且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:检测页面刷新

更新时间:2023-02-26 14:23:19

我已经解决了这个问题……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
?>