Using php to record length of stay

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dan2004
Forum Newbie
Posts: 1
Joined: Fri Sep 09, 2011 2:12 pm

Using php to record length of stay

Post by dan2004 »

Hi,

I have a webpage that when opened contains a jukebox written in flash. Whenever a user opens this page the html triggers a php script that sends me an e-mail with the users ip address. I would like to know how to go about including the length of time that the user spent on the page, actually on the jukebox. Can that be done via php? If so do you think that using php is the best option to do this? If not what suggestions might you have? Really appreciate anyone's help....

Here is basically what the HTML code for the Jukebox page looks like:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Jukebox</title>
<script type="text/javascript" src="audio_player_files/flashdetect.js"></script>
<style type="text/css"></style>
</head>


<body>
</div>
<script type="text/javascript">
var so = new SWFObject("audio_player_files/audioPlayer.swf", "player", "400", "180", "6", "#666666");
so.addVariable("xmlPath", "audio_player_files/data.xml");
so.write("flashcontent");
</script>

<iframe src="siteHitEmail.php" HEIGHT="0" WIDTH="0"></iframe>

</body>
</html>


Here is my php script: (referenced by <iframe src= in the html above)

Code: Select all

<?php
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR); }
$to = "danshirl@aol.com";
$subject = "Jukebox accessed";
$email = $ipaddress;
$message = "";
mail($to,$subject,$message, $email);
?>
Thanks!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Using php to record length of stay

Post by Celauran »

Sounds like something better suited to Javascript. Record the time of the load event and of the unload event. Subtract the former from the latter, and that's how long they spent on the page.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Using php to record length of stay

Post by flying_circus »

It's probably a task best suited for your swf object.

Javascript can be disabled, and an event doesnt get sent to the server (to send you a follow up email) to notify you when the user leaves. However, the flash piece is the runtime for your jukebox, I would write an actionscript that pings your server after every song or every 5 minutes, to update a database or something.

Tracking the length of a users stay is difficult thing to do, because the "good bye" message has to be sent from the client side, something that you wont get control of.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Using php to record length of stay

Post by ok »

You can do something similar to time tracking with PHP, however it involves more code and less accuracy.

Basically you can keep refreshing the iframe, say every 5 seconds, and record with PHP the last refresh time (you have to use sessions for this to work). You might want to record it in a database.
You also need to build a script which runs every given time (say 1 minute). This script checks for the last refresh time. If the last refresh time isn't from the last minute, you take it as a session end event and you send a mail to yourself.

As you already understand this solution is far from ideal.

As said above, javascript/flash is the best way to handle this type of tracking. You can check Google Analytics which has a more accurate algorithm and more tracking features.
Post Reply