A Flash Developer Resource Site

Page 1 of 4 1234 LastLast
Results 1 to 20 of 63

Thread: How Get Time Spent on Webpage? Help, wont solve! over 5 hours :( !!

  1. #1
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337

    How Get Time Spent on Webpage? Help, wont solve! over 5 hours :( !!

    Hey everyone,

    So I have a problem with javascript php and html integration... its a real pain in the a*s..

    So first off I started to look for a way to save the amount of time a visitor spends on your webpage... Well, I found this tutorial after 20 minutes of looking: http://javascript.internet.com/time-...t-on-page.html

    It was real nice, so I tried it. Worked like a charm, but then the hard part was to get its variables (that stored the total amount of time spent) to php, so I could record the data in my SQL Database...

    But-- after an hour of trying to parse the php in js, and other way around and tried alot of crazy scripting things... , I realized you CAN'T send JS vars to PHP!!! So I was sooo confused, then I went to the html part of the tutorial. (You have to put a js script in the <head> and then put a onload and onunload interval statement in the <body>, following the tutorial)

    So I thought, I'll change this js script to php... and it shouldve worked out fine, and I think it did. So I put the php version of tracking time spent in my index.htm (which can parse php). So then I tried calling functions in the <body onload = 'window.setInterval(<?php timeHere() ?>, 100)' onunload = <?php sayTime() ?>> I found out that if i put a test script it'll work...but the problem was, the onload statement. I tried calling the timeHere php function inside setInterval, but it just wouldnt work!

    And I had no way of seeing that my time tracking php script worked, since "onunload" you go to a different page, and if i tried printing back data i wouldnt see it!

    So I've been doing this crap for over 4-5 hours, and no luck.

    So I am asking you, or someone, probably Musicman, or anyone else to help me create a way to record how much time a visitor spends time on a webpage of mine, and then pass the totalTimeSpent var to php.

    Please Help!

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    I realized you CAN'T send JS vars to PHP!!!
    have you ever heard of AJAX? The basic idea seems to be some odd javascripts that talk to a server - and the next idea seems to be to use a framework (some 50 to 100k of script code) to make use of it.
    Here is a little example without a framework - it is simple because I do not care for
    a) browsers not supporting it, like older IE, or other reasons for data lost
    b) results returned - I am leaving anyway
    Code:
    <script>
    function start()
    {       starttime = (new Date()).getTime();
            xmlobj = new XMLHttpRequest();
            xmlobj.open('POST', 'timelog.php');
    }
    function leave()
    {       stoptime = (new Date()).getTime();
            xmlobj.send('spent='+(stoptime-starttime));
    }
    </script>
    <body onload=start() onunload=leave()>
    How long will an idiot stare at this page before leaving it?
    <br><a href=xyz.php>view something else</a>
    Of course you need to trust things, since there is no way to print back the results. How about actually creating a timelog.php that would record the data (time spent, perhaps visitor's ip address, or a cookie) to either a text file or a mysql table?

    Musicman

  3. #3
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Okay im sorry, i dont quite understand that script. Is there an AJAX script that i could use to just send the "finalTime" js variable to a php script? Because I dont know how to use ajax, and dont understand it.

    timelog.php? do you mean make a different way to track time spent on a page? I would love for a way to do that, ex. an easy tutorial that can track amount of a time a visitor spend on page and then send it to sql database.

    Thanks

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    well, that's all the script is about
    onload gets current time, onunload gets current time again, and the difference is sent as a variable "spent"
    The essence of AJAX is that funny xmlhttprequest - a way to send a request to a server and get a response without loading a web page (before, you could have done similar, with js submit()ting a form, but the form would have to target a frame or window that was not about to close.
    Now, how would a timelog.php for a database look?
    Code:
    <?
    // usual connection stuff first, then
    $spent = $_POST['spent'];
    if((string)$spent) !== (string)(int)$spent) exit; // someone is trying to send invalid data
    $ip = $_SERVER['REMOTE_ADDR']; 
    mysql_query("insert into timetable (spent, ip) values ($spent, '$ip')");
    ?>
    Musicman

  5. #5
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Ohhh, thanks! Haha Musican your a genius, by the way i have a just a few more q's.

    1. Basically i have a whole tracking script thing in my index.htm, which parses php. And you know that js code you gave me up there? Should I call 'index.htm' instead of 'timelog.php', because then i could recieve post variables on my index.htm webpage.

    2.That js code, when i open it in a window, www.trixmasta.com/timePage.js, it shows the code itself. This is a noob question, but im in hurry so I asked this. And when I open that page, isnt it supposed to send vars to php which is supposed to write to my database? For some reason it didnt...

    3. Last question, how would I make a framework for ajax?

    Thanks!

  6. #6
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Update: It still wont work

    I have the js in my index.htm, inside my <head> tags, and i have the <body onload and unload> thing, but still wont work. Seems as if the js isnt calling the php correctly. I checked the path names all right...but still the php script doesnt run. I even opened it in the browser, and it worked correctly (if i removed the "if((string)$spent !== (string)(int)$spent) exit; // someone is trying to send invalid data" line)" , then the php script would only record empty rows in the table (since there is no time variable to recieve from js). So the problem is, js isnt sending the vars to php correctly.

    Please help, thanks!

  7. #7
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Update again:

    Sorry for posting twice, but the actual problem is the time variable isnt getting defined. JS does call the php script correctly, the reason is because, i was using google chrome and either it has js turned off or doesnt work cause its beta. So i used IE, and it recorded empty rows in my table again--which is good because that means the js talks to php correctly, but the variable itself wasnt defined.

    Now i tried playing around with getTime objects, but since im a js newbie, i didnt fix anything.

    So now we know this small problem, i bet you could fix it in a sec haha.

  8. #8
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    I was using this exact html page I posted above, and it definitely sent POST requests with a "spent" variable. I am using mozilla browser, but Date should be standard on all browsers.

    Do you have your script online, to take a look?

    Musicman

  9. #9
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Still wont work

    here's my php:

    PHP Code:
    <?php

    $spent 
    $_POST['spent'];
    mysql_connect("localhost""**********""*********") or die(mysql_error());
    mysql_select_db("*************") or die(mysql_error());



    $query mysql_query("INSERT INTO counter (time_spent) values ('$spent')");
    if(
    $query){
    print 
    "yes query works";
    }
    ?>
    and the js on testtime.html:

    PHP Code:
    <script>
    function 
    start()
    {       
    starttime = (new Date()).getTime();
            
    xmlobj = new XMLHttpRequest();
            
    xmlobj.open('POST''timelog.php',true);
    }
    function 
    leave()
    {       
    stoptime = (new Date()).getTime();
            
    xmlobj.send('spent='+(stoptime-starttime));
    }
    </
    script>
    <
    body onload=start() onunload=leave()>
    How long will an idiot stare at this page before leaving it?
    <
    br><a href=xyz.php>view something else</a
    you can also view it at: http://www.trixmasta.com/testtime.html

    Every time I unload that page, it only inputs empty rows in my database, the time varibles are getting defined, because instead of 'stoptime- starttime' i used an actual number, but still no result in db. So the problem must be the way the script sends it?

  10. #10
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    something strange on the php / mysql part: the page definitely sends a "spent" value via post, and the php script says it worked

    Musicman

  11. #11
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    So then whats the problem?

    Because i checked too, php script works. But I dont think the js is sending the variable correctly, because in my database i get empty rows

  12. #12
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Ugh, still cant solve it. Keep getting empty rows

    Please help

    Thanks

  13. #13
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    please help...the same problem is still occuring

  14. #14
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    when I tested, your web page definitely sent a value to the php script. That's about all I can do.
    Did you get any counts in the database? Just one or two from me?
    What kind of browser are you using - I got oldish mozilla (the last one before it got renamed to seamonkey)

    Musicman

  15. #15
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Hi, hmm...well i downloaded wireshark to try and look for packets being sent, but i dont even know how to view the packets. I tried capturing, but no packets found. What do you do with it so you can see vars being passed and packets?

    Okay, i remade the two webpages:
    http://trixmasta.com/timetest.php
    PHP Code:
    <?php

    $spent 
    $_POST['spent'];

    mysql_connect("localhost""*********""******") or die(mysql_error());
    mysql_select_db("*************") or die(mysql_error());


    //if((string)$spent !== (string)(int)$spent) exit; // someone is trying to send invalid data

    $query "INSERT INTO time_test (time_spent) values ('$spent')";
    $result mysql_query($query) or die(mysql_error());

    if(
    $result){
    print 
    "yes query works";
    }
    ?>
    http://trixmasta.com/testJs.html
    PHP Code:
    <script type="text/JavaScript">
    function 
    start()
    {       
    starttime = (new Date()).getTime();
            
    xmlobj = new XMLHttpRequest();
            
    xmlobj.open('POST''timetest.php');
    }
    function 
    leave()
    {       
    stoptime = (new Date()).getTime();
            
    xmlobj.send('spent='+(stoptime-starttime));
    }
    </
    script>
    <
    body onload=start() onunload=leave()>
    How long will an idiot stare at this page before leaving it?
    <
    br><a href=xyz.php>view something else</a
    I tried with 3 different browsers, Mozilla Firefox 3.0.5, IE7, and Google Chrome. All of them recorded EMPTY rows in my database, except google chrome which didnt record anything.

    Here is a online table of the database,
    http://www.trixmasta.com/test_time_table.php
    you can see each empty row everytime you run the html page with the javascript. I dont know what more info i can give, but do you have an idea why it wont send the var? It should work...this problem is soo annoying!

    Thanks, please help!

  16. #16
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Can you paste here your database schema? It's POSTing the 'spent' variable correctly, and returning the "yes query works". So something is going on when you're inserting it to your database.

    Also on a side note, you should put some quotes around your onload and onunload statements.
    Code:
    <body onload="start()" onunload="leave()">
    That's probably why Chrome isn't recording anything.
    Last edited by MyFriendIsATaco; 01-05-2009 at 07:05 PM.

  17. #17
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Wait, just wondering...how do you know if its is posting correctly? Wireshark? if so can you explain how i can use it to monitor sending the vars and stuff?

    Well I'm not sure if this is what you want, but i went in information_schema, and clicked print view:


    Table Records Type Size Comments
    CHARACTER_SETS in use
    COLLATIONS in use
    COLLATION_CHARACTER_SET_APPLICABILITY in use
    COLUMNS in use
    Creation: Jan 05, 2009 at 04:17 PM
    Last update: Jan 05, 2009 at 04:17 PM
    COLUMN_PRIVILEGES in use
    KEY_COLUMN_USAGE in use
    PROFILING in use
    ROUTINES in use
    Creation: Jan 05, 2009 at 04:17 PM
    Last update: Jan 05, 2009 at 04:17 PM
    SCHEMATA in use
    SCHEMA_PRIVILEGES in use
    STATISTICS in use
    TABLES in use
    TABLE_CONSTRAINTS in use
    TABLE_PRIVILEGES in use
    TRIGGERS in use
    Creation: Jan 05, 2009 at 04:17 PM
    Last update: Jan 05, 2009 at 04:17 PM
    USER_PRIVILEGES in use
    VIEWS in use
    Creation: Jan 05, 2009 at 04:17 PM
    Last update: Jan 05, 2009 at 04:17 PM
    17 table(s) 0 -- 0 B


    Oh by the way, chrome still wont work I guess thats cause of the AJAX?
    Last edited by flashtrickster; 01-05-2009 at 07:31 PM.

  18. #18
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    That's not the information I need. I'm assuming you're using phpMyAdmin.

    Just do an export of the time_test table. I want to see what all columns are in there, and what the types are.

    Oh, and I use Firebug to see the HTTP requests. It's a plugin for Firefox.

  19. #19
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Oh okay, i uploaded it here : http://trixmasta.com/time_test.sql

    Cool, I'll check it out.

    Hmm, thats the strangest thing, it says sends the var by posting and yes query comes back, but then it inserts empty rows in the table?
    Last edited by flashtrickster; 01-05-2009 at 07:54 PM.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  20. #20
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Change your PHP to this:
    PHP Code:
    <?php print_r($_POST); ?>
    I would just like to see what is coming up.

    Also, what version of PHP is on your server?

    [edit]Nevermind, it's PHP 5.2.8[/edit]
    Last edited by MyFriendIsATaco; 01-05-2009 at 08:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center