Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2005, 09:53 PM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP calling JavaScript onUnload

This seems so simple, but I can't figure it out. When someone visits my index.php page, I log the time they arrived with a simple PHP script. I want to simply log the time they exit or close the window, and write it to the same logfile. I can't figure out how to make this work.

I considered just calling the same scripting file again to just write the most current time into the same file, but I can't figure out how to make it run the script again. I'm strong with the JavaScript, but very weak with PHP.

Seems the onUnload command is my only hope, but JavaScript doesn't support PHP. I'm pretty sure PHP supports JavaScript, though.

Thanks for the input!
Mike L
 
Old November 30th, 2005, 10:21 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

onunload isn't reliable, in fact it may not finish executing before the page is unloaded. What you want is the onbeforeunload event, and at that, it probably won't be what you expect. It is intended as a last ditch effort to warn the user about saving something before exiting a document. It causes a confirmation dialogue to appear which cannot be circumvented. It was previously a MS propietary feature that has since been adopted by Mozilla Firefox, and Safari (I think). Not sure about Opera.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old November 30th, 2005, 11:08 PM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So, it sounds like your telling me my efforts are futile and I should get on with my life, huh?! :-|

Darn. Oh, well. Maybe somebody someday will invent something. It would be a VERY beneficial to many e-commerce sites- even just a simple innocent timestamp.

Thanks!!

 
Old January 17th, 2006, 10:24 AM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a bit late, I know, as I only just now came to this forum on a random chance.

My suggestion is to do what Gmail does. Every one minute, save the current time. Then you can tell when the user left the website within 1 min. accuracy. If you need a greater or less accuracy, then just adjust the time interval.

To do this with PHP and Javascript you'll need to use the popular "AJAX" technique.

2 good sources for "AJAX" are.

http://www.phpbuilder.com/columns/kassemi20050606.php3
http://www.pixel2life.com/twodded/t_...to_send_data_/

in quick Pseudo code it would look like thing like this.

PHP timeDBSave.php
Code:
   $Timestamp = $_REQUEST['timestamp'];

   $query = "INSERT INTO MyTable Timestamp = timestamp";
   executeQuery($query);
Code:
   Every x seconds
   {
      Execute_XMLHttpRequest(timeDBSave.php);
   }
Sorry, I don't have time to execute the pseudo code for the "AJAX" part, but you can easily check out the examples in the above two links to understand that.

Cheers,
Fozzy






Similar Threads
Thread Thread Starter Forum Replies Last Post
calling template with javascript smys123 XSLT 3 November 22nd, 2007 04:13 AM
Calling a javascript function from C# SebFr C# 0 November 3rd, 2006 12:05 PM
Calling WS from javascript r_ganesh76 Javascript 5 December 8th, 2004 05:31 AM
calling javascript from flash Adam H-W Flash (all versions) 2 March 17th, 2004 11:26 AM
calling webservices from Javascript.. wb25 J2EE 1 February 20th, 2004 10:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.