我怎么知道页面是否在浏览器中运行?

时间:2013-10-22 11:52:25

标签: php

如何知道页面是在浏览器中真正打开还是从脚本或src =“xxx”获取?

<?php 
session_start();
if(!isset($_SESSION['count'])){
    $_SESSION['count']=0;
}

// I don't want to
// if(open in brower){
    $_SESSION['count']++;
// }
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $.ajax({
        url:'index.php',
        type:'GET',
        data:{},
        success:function(){}
    });
</script>
<?php 
    echo $_SESSION['count'] ; // the result now is 1,3,5,... for every reload
    // I want to get the result 1,2,3,4... for every reload , how to check the page is really open in brower? not run in ajax or get ?

?>

1 个答案:

答案 0 :(得分:1)

这个具体示例的一个简单解决方案是将一些数据传递给ajax请求,并检查数据是否存在以增加会话数。

<?php 
session_start();
if(!isset($_SESSION['count'])){
    $_SESSION['count']=0;
}

if(!isset($_GET['dontcount'])){
    $_SESSION['count']++;
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $.ajax({
        url:'index.php',
        type:'GET',
        data:{'dontcount',1},
        success:function(){}
    });
</script>
<?php 
    echo $_SESSION['count'];

?>