开发者

Browsed Time Problem

I want to display the browsed time of a user, But when i refresh it, it will be again start from 0:0:0. How can it handle?

<?php
 $total_mints=($live_match['match_name']) * (60);

?>
<script language="javascript">
display_c(<?=$total_mints?>,'ct');
</script>

<sc开发者_如何学编程ript type="text/javascript">
function display_c(start,div){

window.start = parseFloat(start);
var end = 0 // change this to stop the counter at a higher value
var refresh=1000; // Refresh rate in milli seconds
if(window.start >= end ){

mytime=setTimeout("display_ct('"+div+"')",refresh)
}
else {alert("Time Over ");}
</script>


Once the time is over, you could set a cookie to 'Time Expired'... When the page is loaded, if the cookie is 'Time Expired' then you can display the 'Time Over' alert. You can also use the cookie to keep track of accumulated browsing time.

Edit - added some specifics... but I think you'll have to think about this some more.

Basically, you want to use JS to write the cookie as the user uses the page, and you want to use PHP to read the cookie when the page is loaded. You can use the cookie to either only track whether time is up, total accumulated time, or both. I think you'd want to renew the cookie every minute or so?

It's going to look SOMETHING like this - this code just shows how to keep track of whether time has expired or not with a cookie, not accumulated time.

<?php
$total_mints=($live_match['match_name']) * (60);
// check for cookie and only proceed if it is not expired
// can also use cookie to keep track of total accumulated number
// of minutes between session
if ($_COOKIE["yourMints"] != "expired")
{
    ?>
    <script language="text/javascript">
        display_c(<?php echo $total_mints; ?>,'ct');
    </script>
    <script type="text/javascript">
    function display_c(start,div)
    {
        window.start = parseFloat(start);
        var end = 0 // change this to stop the counter at a higher value
        var refresh=1000; // Refresh rate in milli seconds
        if(window.start >= end )
        {
            mytime=setTimeout("display_ct('"+div+"')",refresh)
        } else 
        {
            alert("Time Over ");
            // set cookie to expired
            document.cookie = "yourMints=expired";  
        }
    }
    </script>
    <?php
} else // What follows is what happens if cookies IS expired
{
    ?>
    <script type="text/javascript">
        alert("Time Over ");               
    </script>
    <?php
}
?>

Here is a good JS cookies tutorial: http://www.quirksmode.org/js/cookies.html

Here is using $_COOKIE to read cookies with PHP http://php.net/manual/en/reserved.variables.cookies.php

EDIT: Added in JQuery example after seeing PlagueEditor's example.

Nice script PlagueEditor. Thought I'd try the same thing w/ JQuery for fun.

JQuery has a simple little cookie plugin... only 40 lines of code or so. Here's a page with a cookie stored timer and a timeout of 10 seconds with a possible reset:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Time Spent on Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="PATH-TO-YOUR-JQ-DIRECTORY/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="PATH-TO-YOUR-JQ-DIRECTORY/cookie.js"></script>
        <script type="text/javascript">
        <!--
            $.myTimer = 
            {
                timeLimit: 10,
                displayTime: function ()
                {
                    if ($.myTimer.time < $.myTimer.timeLimit)
                    {
                        $("#timeHere").html($.myTimer.time);                 
                        $.cookie('yourMints', $.myTimer.time, { expires: 7});     
                        ++$.myTimer.time;
                        $.myTimer.toggle = setTimeout("$.myTimer.displayTime()",1000);
                    } else
                    {
                        $("#page").html('<h1>Time expired</h1>');
                    }
                }
            }
            // When the page is ready ==================================================
            $(document).ready(function()
            {
                // Read time spent on page cookie. Set it, if it doesn't exist.
                if (!$.cookie('yourMints'))
                {
                    $.cookie('yourMints', '0', { expires: 7});                 
                }
                $.myTimer.time = $.cookie('yourMints');
                // Start timeer
                $.myTimer.displayTime();
                // Reset the timer
                $("#reset").click( function() 
                {        
                    $.cookie('yourMints', '0');
                    window.location.reload();
                });
            });
        // -->
        </script>
    </head>
    <body>
        <div id="page">
            <h2>Your total time here: <span id="timeHere"></span></h2>
            You can only look at this page for 10 seconds.
        </div>
        <input id="reset" type="button" value="Reset Timer" /> 
    </body>
</html>


Below is a solution for keeping track of the browsed time, even with refreshing. It gets the date when the page loads and every second subtracts that date from the given date. The date is then displayed in the span. The page should work by itself. I hope this is what you were looking for, or at least helps. Two functions were W3Schools examples*.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <script type="text/javascript">
            /**
             * getCookie and setCookie were taken from http://www.w3schools.com/JS/js_cookies.asp.
             */
            function getCookie(c_name)
            {
                if (document.cookie.length>0)
                {
                    c_start=document.cookie.indexOf(c_name + "=");
                    if (c_start!=-1)
                    {
                        c_start=c_start + c_name.length+1;
                        c_end=document.cookie.indexOf(";",c_start);
                        if (c_end==-1) c_end=document.cookie.length;
                        return unescape(document.cookie.substring(c_start,c_end));
                    }
                }
                return "";
            }

            function setCookie(c_name,value,expiredays)
            {
                var exdate=new Date();
                exdate.setDate(exdate.getDate()+expiredays);
                document.cookie=c_name+ "=" +escape(value)+
                    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
            }

            var totalTime=0;

            var storedTime=getCookie("storedTime");
            if(storedTime.length == 0){
                //If it doesn't exist..
                storedTime=0;
            }else{
                storedTime=parseInt(storedTime);
                totalTime=storedTime;
            }

            function updateTime(){
                totalTime+=1000;
                document.getElementById("duration").innerHTML= Math.ceil(totalTime / 1000);
            }

            onbeforeunload = function(){
                setCookie("storedTime",totalTime,3);
            }            
            setInterval(updateTime, 1000);
        </script>
        Your total time here: <span id="duration"><script type="text/javascript">document.write( Math.ceil(totalTime / 1000));</script></span> seconds...
    </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜