A Flash Developer Resource Site

Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 63

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

  1. #21
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Hmm, as a response now, it's: yes query works, Spent=

    Spent= being empty. Are you echoing the $spent variable?

    If so, try changing $spent = $_POST['spent'] to $spent = $HTTP_POST_VARS['spent']

  2. #22
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    PHP version is: 5.2.8
    Also, I was trying different things with the php script, and i tried GET, just to see if it would insert correctly...and it did it shows up in the table. So the problem is not inserting, its recieving the vars like I've been saying this whole time. Hmm, because now i also added ",Spent= $spent" to the print "yes query works".

    Okay just changed the script to:
    <?php print_r($_POST); ?>
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  3. #23
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Yeah, for some reason $_POST is an empty array.

    Try:
    PHP Code:
    <?php print_r($HTTP_POST_VARS); ?>

  4. #24
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Okay did that... same thing is happening
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  5. #25
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Ok. After digging around, I wrote a quick little form to post a variable manually, and it worked just fine.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
    	<title>Form Test</title>
    	
    </head>
    
    <body>
    
    <form action="http://trixmasta.com/timetest.php" method="post">
    	
    	<p><input type="text" value="100" name="spent" /></p>
    	<p><input type="submit" value="Continue &rarr;" /></p>
    </form>
    </body>
    </html>
    So, your PHP script is fine. It has to be the Javascript sending the variables, which is weird, because it shows up in Firebug correctly, but apparently not.

    I'm not a pro Javascript coder, so I'm not sure how to narrow down that issue. Anything that I do with Javascript is through jQuery, and jQuery has ajax functions built in to handle all that stuff.

    So, the bottom line is, your PHP script is fine. The Javascript to send the variables is a bit loopy. I think for once Musicman may have pointed ya in the wrong direction. :/

    As a final resort, revert your PHP code back, and change your Javascript functions to this:
    PHP Code:
    function start()
    {
            
    starttime = (new Date()).getTime();
    }
    function 
    leave()
    {
            
    stoptime = (new Date()).getTime();
            var 
    params "spent="+(stoptime-starttime);
            
    xmlobj = new XMLHttpRequest();
            
    xmlobj.open('POST''timetest.php'true);
            
    xmlobj.setRequestHeader("Content-type""application/x-www-form-urlencoded");
            
    xmlobj.setRequestHeader("Content-length"params.length);
            
    xmlobj.setRequestHeader("Connection""close");
            
    xmlobj.send(params);

    Last edited by MyFriendIsATaco; 01-05-2009 at 09:05 PM.

  6. #26
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    You're a genius! Haha, thats great Im so happy now... this annoying problem is fixed, but I'm just wondering how did this js differ than the one Musicman gave?

    Haha musicman was probably busy, can't blame him he's helping around all the time.

    Hmm, just doubled check. And for some reason when I press load response in firebug, it doesnt echo back the $spent var. And in the table http://www.trixmasta.com/test_time_table.php the time_spent is recorded but for some reason it adds an empty row...
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  7. #27
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    That's not exactly how to use Firebug. It's a weird little app that takes a long time to get used to all the details.

    I added some request headers. The Content-type and Content-length. Also, the third parameter in xmlobj.open('POST', 'timetest.php', true); was missing in your script. It was present in his. The true tells the script to make an asynchronous request. I don't know if that would break it, but it should be there.

  8. #28
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Ohh...that makes sense, but could you explain why there is an extra row being added in the table everytime? I'm stumped.

    But when i add the:
    if((string)$spent !== (string)(int)$spent) exit; // someone is trying to send invalid data

    it eliminates the empty row, but still why would an empty row be added?
    Last edited by flashtrickster; 01-05-2009 at 09:31 PM.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  9. #29
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    That doesn't appear to be the case when I'm checking it out.

    It'll probably insert an empty row if you just go to timespent.php in your browser.

    To eliminate that, just put at the top of the script:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] != 'POST') exit; 
    That just means if the file wasn't POSTed to, it exists.

  10. #30
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Ahhhhh wait I know why! It's cause i clicked "Load Response", haha that makes sense.

    Oh by the way, i'm going to be putting this js code on each webpage that has a php tracking script. And in that tracking script, all the userdata is recorded at once. But I want the time_spent to be recorded within that row, but how can I get that done if you can only get time_spent when the page unloads?

    Thanks for help and troubleshooting
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  11. #31
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    That's where cookies and sessions come into play. It's a whole other ballpark.

  12. #32
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Do you have a source/tutorial for me too look into and learn that stuff, or get an idea?

    Thanks
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  13. #33
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Actually nevermind, i found one on tizag...
    But would cookies and sessions really be the right solution? Or is there quick way.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  14. #34
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    The point is to tie a specific id string to a persons computer so you can track what they do. That id can either be stored in a cookie or in a session variable. The most common is probably a cookie.

    That id would then relate to a row in your database somehow and you can do whatever you want with it.

    But the real question here is... why aren't you just using an already tried and true statistics tool? Like Google Analytics?

  15. #35
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Ohh, I was actually thinking something along the lines of that. Maybe tell timetest.php to insert the time_spent in the row with the same last ip address and page. But then that could possibly not work, if multiple windows are open or many people are online at once.

    Haha, well I already have google analytics and clicky on my website But, google anlaytics lacks one feature-- recording ip addresses. And I need to have a way to differ each visitor from each other. Plus, I wanted to be able to create my own tracking script, of course with help from you guys when there is a script problem haha.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  16. #36
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Different IPs don't necessarily mean different people.

    But Google Analytics does record IP addresses, just not right out in the open. It's tracked in the form of "unique hits".

    Unique visitors is exactly what you want.

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

    one of the - at least controversial - things about google analytics: in the end google knows much more about your visitors than you do

    Musicman

  18. #38
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    thats true...but wait how would i get a unique visitor? by using cookies? Because thats what i'm doing

    Hey musicman, i never knew that! Ha another reason I should make a custom tracking script, haha.

    I am actually working on it..but i hope my theory works, correct me if i'm wrong:

    1.Person visits www.trixmasta.com
    2.index.htm runs a php script that gathers ip addresses, broswer, site referal, and assigns an id number (auto increment) to the row with the visitor info. Then by using mysql_insert id(), it gets that id number and creates a cookie. That cookie, has the id number, and is set to expire when the session has ended. THEN when the browser unloads, the js sends time spent to the php script, which stores that time spent in a variable. Then it reads computer for the cookie name created earlier, and reads the id number. It the queries "UPDATE table SET time_spent= '$timespent' WHERE id= '$id'". That will basically put the time spent var in the correct row with user data.

    Is my idea correct, as in would it make sense to work? The only thing im confused about is the expiration date for the cookie, i made it to be when the session ends (or when the browser is closed). Because would opening multiple windows or other things like that, mess up the ids?

    Thanks
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  19. #39
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Okay I just tested, and finished my idea above and it works great
    The only problem is (there's always some issue) that Google Chrome still does not record the time_spent, probably due to AJAX? I have checked chrome, javascript is enabled... hmm i just dont know what would cause Chrome to not record the time spent variable.

    Anyways thank you all for your help
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

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

    two things google does differently
    - they try to set a cookie with 1 month lifetime, so unless someone has turned cookies off or runs a cookie cleaner, unique visitors is really what it means - regardless of people sharing an ip address, or using dynamic ip address.
    - their "time spent on site" is derived from clicking links within the site that also have a google code applied to them

    Isn't Chrome still beta? maybe the developer team has a solution or takes a bug report...

    Musicman

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