Jump to content

I'm trying too count total visited time.


andrababiz

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.