andrababiz Posted May 8, 2014 Share Posted May 8, 2014 Hello I'm trying too make a function that will count how long a user is on my page and when he leavs, saves it in my database. I'll admit, that I'm totaly new too ajax, but I want too learn. Can someone please take a look at my code and maybe point me at the right direction dunkapa.php, this is the site that I want too count time on. <head> <script> var startTime = new Date(); window.onbeforeunload = function() { var endTime = new Date(); //Get the current time. var timeSpent = (endTime - startTime); //Find out how long it's been. var ip = (window.location != window.parent.location) ? document.referrer: document.location; $.post('ajaxtime.php', {ip: ip, timeSpent: timeSpent}); }; </script> </head> ajaxtime.php <?php $mysql_hostname = "*****"; $mysql_user = "*****"; $mysql_password = "*****"; $mysql_database = "*****"; $prefix = ""; $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database"); mysql_select_db($mysql_database, $bd) or die("Could not select database"); $tid = $_POST['timeSpent']; $sql_in = "insert into `TID` (TID) values ('$tid')"; mysql_query( $sql_in); ?> I have asked around a bit but no one can help me, with the ajax, but it seams like the javacode is correct. Quote Link to comment https://forums.phpfreaks.com/topic/288345-im-trying-too-count-total-visited-time/ Share on other sites More sharing options...
sKunKbad Posted May 10, 2014 Share Posted May 10, 2014 I don't think what your trying to do is possible. I understand usage of window.onbeforeunload, but don't believe that it's meant to be used for what you're attempting to do. As far as I know, window.onbeforeunload is only going to give the site visitor a chance to stay of leave, based on a suggested message. For instance, "Are you sure you want to leave, because if you do the world will self destruct.". Maybe I'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/288345-im-trying-too-count-total-visited-time/#findComment-1479026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.