duanhao8540 2011-06-04 07:18
浏览 57
已采纳

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

Can any one help in storing the time spent by user on a particular page... This is what I did... It seams to work but i don't know my my php page is unable to get the data and store in database... individually while I am calling StoreTime.php?time=23 it is getting the time value and storing the data...

<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条回答 默认 最新

  • dongshaoxiong0012 2011-06-04 07:26
    关注
    <!-- 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! 
     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()">
    

    Here is some code on this site. Now as you can use this script to calculate the time spent, on the other side of the coin, you can use AJAX to continuosly send the time duration to a php file the server for futher workout with the data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line