Bentley4 Posted June 9, 2011 Share Posted June 9, 2011 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/ Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1227336 Share on other sites More sharing options...
Bentley4 Posted June 9, 2011 Author Share Posted June 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1227352 Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 if you use time() all you need to do is substract endTime - startTime and you'll get the result in total seconds. Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1227403 Share on other sites More sharing options...
Bentley4 Posted June 10, 2011 Author Share Posted June 10, 2011 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'. Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1227878 Share on other sites More sharing options...
WebStyles Posted June 10, 2011 Share Posted June 10, 2011 of course it works. its just a matter of how you store the timestamp. $_SESSION will do. Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1227921 Share on other sites More sharing options...
Bentley4 Posted June 11, 2011 Author Share Posted June 11, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1228314 Share on other sites More sharing options...
wildteen88 Posted June 11, 2011 Share Posted June 11, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1228322 Share on other sites More sharing options...
Bentley4 Posted June 11, 2011 Author Share Posted June 11, 2011 Works, awesome Wildteen88! Quote Link to comment https://forums.phpfreaks.com/topic/238861-total-time-spent-on-site-wo-using-mysql/#findComment-1228337 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.