在php中的页面会话,我想存储用户在特定页面上花费的时间......?

时间:2011-06-04 07:18:57

标签: php javascript jquery

任何人都可以帮助存储用户在特定页面上花费的时间...... 这就是我做的...... 接缝工作,但我不知道我的PHP页面无法获取数据和存储在数据库中... 当我调用StoreTime.php时,它会单独获得时间值并存储数据......

<html>
<head>
    <title>
        Page time
    </title>
</head>
<script type="text/javascript">

    var time=1;

    function timeHere() 
    {
        time = time + 1;
        finalTime = time / 10;
        document.title = finalTime;
    }

    function sayTime() 
    {
        finalTime = time / 10;
        showTime(finalTime);
        alert("U are in this page for " +finalTime+" Sec");
    }


    function showTime(finalTime)
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","StoreTime.php?time=" +finalTime,true);
        xmlhttp.send();
    }

    function sendTimeSpent(finalTime)
    {
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET","/Test/Timer/StoreTime.php?time="+finalTime,true);
        xmlhttp.send(null);
    }
</script>   
<body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()">
    Close the page to see the time spent on this page...
    <div id='myDiv'></div>
</body>

1 个答案:

答案 0 :(得分:2)

<!-- Paste this code into an external JavaScript file named: timePage.js  -->

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Cody Shaffer :: http://codytheking313.byethost11.com */

var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;
/* Remove the "//" below to display in the title bar
  the amount of time the visitor has been on the site.
  Be aware though, that it does tend to be a bit distracting. */
// document.title = finalTime+" seconds you been here for!";
}

function sayTime() {
  finalTime = time / 10;
  alert("Thank you for coming to my site! \n You have been here " + finalTime + " seconds!");
}



<!-- Paste this code into the HEAD section of your HTML document.
     You may need to change the path of the file.  -->

<script type="text/javascript" src="timePage.js"></script>



<!-- Paste this code into the BODY tag -->

<body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()">

以下是this网站上的一些代码。现在,您可以使用此脚本计算花费的时间,在硬币的另一侧,您可以使用AJAX将持续时间连续发送到服务器的php文件,以便进一步处理数据。