Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 30th, 2005, 09:53 PM
Registered User
Join Date: Nov 2005
Location: Phoenix, AZ, USA.
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
Reply With Quote
  #2 (permalink)  
Old November 30th, 2005, 10:21 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

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.


Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote
  #3 (permalink)  
Old November 30th, 2005, 11:08 PM
Registered User
Join Date: Nov 2005
Location: Phoenix, AZ, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

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.


Reply With Quote
  #4 (permalink)  
Old January 17th, 2006, 10:24 AM
Registered User
Join Date: Jan 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

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.


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

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

   $query = "INSERT INTO MyTable Timestamp = timestamp";
   Every x seconds
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.


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 05:56 AM.

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