Jump to content

Total time spent on site w/o using MySql


Bentley4

Recommended Posts

Hi guys!

 

I would like to know the total time someone spent on my website by saving it into a textfile. Is this possible? I've used this code so far. When the form is filled in, I want to know the time spent on filling in the form and submitting it(real code is longer).

This code is not good because it just displays the time when a user visits the last screen(I understand why). But If I declare it above the if statement, it returns a totaltime of 0:0:0.(I also understand why, I just don't know any solution to the problem.) Is there any solution without using MySql?

 

<?php

$name = $_GET['sid'];
if(!isSet($_GET['sid'])){

   $Hin = date("H");
   $Min = date("i");   
   $Sin = date("s");
   $timein = $Hin.":".$Min.":".$Sin ;
   $date = date("D d M Y");
   $fp = fopen("formdata.txt", "a");
   $savestring = $name . ", ".$date. ", " . $timein . "\n";
   fwrite($fp, $savestring);
   fclose($fp);

   echo '<form name="input" action="./StudentAnswers.php" method="GET">';
   echo '<input type="text" name="sid"/>';
   echo '<input type="submit" value="Submit">';
   
   }

else{
   echo "End of exercices";
   
   $Hout = date("H");
   $Htot = $Hout - $Hin;
   $Mout = date("i");
   $Mtot = $Mout - $Min;   
   $Sout = date("s");
   $Stot = $Sout - $Sin; 
   
   $TotalTime = $Htot.":".$Mtot.":".$Stot ;
   $fp = fopen("formdata.txt", "a");
   $savestring = $name . ", total time: " . $TotalTime. "\n";
   fwrite($fp, $savestring);
   fclose($fp);
   } 

?>

Link to comment
Share on other sites

of course, you could consider using awstats or even google analytics to get that information.

 

Your main problem will not be storing the start time. you can use the unix timestamp for that time(); to store the exact time a user logged in.

The hard(er) part if figuring out when the user left, if they just close the browser, or navigate away instead of hitting the logout button, you have to do constant checks at given intervals to check if they're still online or not. So there will always be a margin of error, the same size as your interval for checking.

Link to comment
Share on other sites

Hi WebStyles,

 

I want to do it without using awstats or google analytics. Just with php code and without using MySql.

I know I can store the startdate. I just don't know how to subtract the time form the first screen from the time the user comes on the last screen.

I was also thinking along the lines of using fget or fread, but I don't know how to recall information from a specific line or next to a certain word.

Link to comment
Share on other sites

Well, that just doesn't work.

The complete code for several screens is in 1 page.(because I used get)

If you do starttime - endtime it just returns 0 because for each screen the code completely runs.

It doesn't work either if you place the starttime under an a certain event using 'if'.

Link to comment
Share on other sites

Ok, I'm sorry. Thnx for the tip.

I've implemented the session construct but it still doesn't work appropriatly.

Does anyone know what is wrong with the following code? :

<?php
session_start(); 
$_SESSION['time'] = time();
$starttime = $_SESSION['time'];
?>

<html>
<body>
<?php
if(!isSet($_GET['sid'])){
   echo '<form name="input" action="./StudentAnswers.php" method="GET">';
   echo '<input type="text" name="sid"/>';
   echo '<input type="submit" value="Submit">';
   }
else{
   echo "End of exercices";
   $endtime = time();
   $totaltime = endtime - starttime;
   echo "Total time: ".$totaltime;  
   } 
?>
</body>
</html>

Link to comment
Share on other sites

The problem is with these two lines of code

$_SESSION['time'] = time();
$starttime = $_SESSION['time'];

When the form is submitted you are overwriting the $_SESSION['time'] variable with a new time. You only want to set the $_SESSION['time'] variable when the form is displayed. And get the start time when the form has been submitted.

 

To fix this first move this line $_SESSION['time'] = time(); so it is placed after this line if(!isSet($_GET['sid'])){.

 

Remove the $starttime = $_SESSION['time']; line. You don't need that there.

 

change the else statement to

else{
   echo "End of exercices";
   $endtime = time();
   $totaltime = $endtime - $_SESSION['time'];
   echo "Total time: ".$totaltime;
   } 

 

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.