thecard Posted December 11, 2008 Share Posted December 11, 2008 What would be the best way to log the timestamp of a user's entrance and exit into a mysql database? Would I have to use javascript? If so which javascript triggers? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/ Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 Sessions would work. What kind of information do you want, just time of entrance and exit? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712699 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Yep, but I have to do it remotely: A user would include an iframe of the php file in their page. Could this still log them? Thanks for the speedy response! Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712701 Share on other sites More sharing options...
s0c0 Posted December 11, 2008 Share Posted December 11, 2008 JavaScript would not be an option, and JavaScript triggers do not exist. Do you want to log when a database user connects to the database or when a user defined in one of your tables (like the user of your application) logs in? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712704 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Create a "image file" that displays a blank white space. Have the user call to it by <img src="http://yoursite.com/img.php"> Inside the img.php file add the code to log data to the database, then spit out a blank image using the image create functions. Should work like you want it without having a nasty iframe and the user does not know it is happening. The user can even set the style.display property on the image to none and yea. <img src="http://yoursite.com/img.php" style="display: none;"> Then it is not even viewable period, but you still get the data logged. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712705 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 But I don't understand how the php will know when to log the timestamp when the user leaves the site I want it to happen to ALL viewers of the site, they don't have to register or anything. I want to know exactly how long each stays Again thanks for all this help. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712713 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 But I don't understand how the php will know when to log the timestamp when the user leaves the site I want it to happen to ALL viewers of the site, they don't have to register or anything. I want to know exactly how long each stays Again thanks for all this help. Impossible. Reasoning is that the user can just close the browser and the browser does not care to send a signal to your site or their site. <body onUnload="" would work as long as they actively left your site view a link or back (I think back/forward to) buttons. But if they typed in the URL manually, I do not think this would work. Unfortunately that part of the request, is impossible to do. You can however set a timeout, after x minutes of no response set them as being out of the site, if they ping the image again within x minutes reset the time stamp to be x more minutes etc. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712720 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Thanks, ok. How do I set this timeout? If you can't be bothered to explain just point me to the correct page of the documentation pls! Always makes a good read Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712722 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 In your database have a timestamp, lastchecked (something like that). Each time the user hits the page, set this timestamp to the current time. To set them as being timed out you would probably need a cron job to run every x minutes that checks the timestamp in this table, and if lastchecked + 5 < current time then set a field as them being inactive. If you want to also keep track of how long they have been on the site you may want a structure like so: DB Fields - lastchecked - timeentered - totaltime When they first enter the site (timeentered should be null) set lastchecked. If timeentered is not null just updated last checked (since the cron job runs every minute or 2 like it should this should update itself properly). In the cron job you would need to do the check, if lastchecked + 5 < current time grab timentered and subtract lastchecked - timeentered, this will give you the total number of seconds the user was on the site, now update the database, set timeentered equaled to null and add the total number of seconds for this session to the totaltime field. This will get you within +- x minutes the total time the user was on the site. I use +5 above that is assuming x minutes is 5 minutes. That is the logic for it, if you want code examples I would google, "Whos Online PHP Script" and you should find many examples of similiar setups (they may not use cron jobs but cron jobs will ensure the +- 5minute rate, without the cron job you cannot be sure, cause they could have been off for 30 minutes and if no one hit the site to reset the timeentered it would show them as being on that whole time). Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712727 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Thanks a lot, I'll look through some examples now. What I really want is just over all time viewed for the site though, it doesn't matter about logging it as indiv. users. Would this make the php much less complicated? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712729 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Thanks a lot, I'll look through some examples now. What I really want is just over all time viewed for the site though, it doesn't matter about logging it as indiv. users. Would this make the php much less complicated? About the same, instead of updating the user_logged table, you would update a field in site_x table that has total time. Same logic, you just change that one part. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712733 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Awesome. Thanks. How do I do a cron check? Again, pls point me if u cba. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712736 Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 You can detect when the user exits the browser. Found this example on another site: Exit detector <br /> var linkClicked = false;<br /> function exitPage()<br /> {<br /> if (!linkClicked)<br /> {<br /> window.alert('You would need to execute your logging code in here');<br /> }<br /> }<br /> Click here to leave this page Click here to leave this page without flagging that you are Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712737 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Dunno, would that work? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712739 Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 Why not? When they exit you can execute AJAX code to log when they left. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712741 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 But would this work if the user left any way shape or form? Like clicking the cross? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712743 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Oh it does! I thought you said this wouldn't work premiso? Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712746 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Weird...I just tried it an it worked. I could have sworn that it did not use to work due to the browser close...I will re-research that, but after testing that it worked on my end so yea. I will verify my research and see where I came up with that. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712750 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Yeah, thanks for your help anyways! Actually tbh, when I started this thread I was pretty sure something as simple as this would fail. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712753 Share on other sites More sharing options...
Mark Baker Posted December 11, 2008 Share Posted December 11, 2008 Not every browser supports onUnload, so it will work with some and not with others Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712760 Share on other sites More sharing options...
thecard Posted December 11, 2008 Author Share Posted December 11, 2008 Know which it works in and doesn't? It appears to work in ie7 and firefox. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712763 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Maybe that was where I was having the issue, is that some browsers would not do this, so it was not fail-safe and I wanted it to be fail-safe. But you could do the cron job to cleanup anything past x-minutes and use ajax to send a call to the page onunload to set the database. A mixture of the two would yield the best results so you have both. Another issue with just using the javascript onunload, is like for instance me in firefox. I tend to have multiple windows and multiple tabs. I could open a new tab and goto a different site and forget about your site being loaded, then it would seem like i was active on the site until I come back to it and close or refresh it. But yea, that is just my opinion a mixture of the two would probably be the best way to get near accurate results. EDIT: As far as which support it, I am not sure, check Opera, Chrome and Safari. I think earlier versions of IE6 did not support it. Also, if the user has javascript disabled, the onunload would not work, but the image file would still work with the timeout, another reason why to use the img with a cronjob at least as the base then the javascript as a bonus, if the javascript fails you still have the img.php standalone code that will keep you straight. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712765 Share on other sites More sharing options...
Mark Baker Posted December 11, 2008 Share Posted December 11, 2008 Know which it works in and doesn't? It appears to work in ie7 and firefox. Among other, IE6 with Google Toolbar according to [urlhttp://www.michaelteper.com/archive/2004/04/28/163.aspx]this blog entry[/url] Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712769 Share on other sites More sharing options...
Maq Posted December 11, 2008 Share Posted December 11, 2008 Or, you could just do a browser conditional and use an IE-specific function instead: Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712770 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 Just remember that user's internet connection may just go down, so don't rely only on this script. There still has to be some timeout in place. Quote Link to comment https://forums.phpfreaks.com/topic/136539-logging-entrance-and-exit-in-database/#findComment-712774 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.