Jump to content

Logging entrance and exit in database


thecard

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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(&#039;You would need to execute your logging code in here&#039;);<br />
        }<br />
      }<br />
    
  
  
    
Click here to leave this page
    
Click here to leave this page without flagging that you are
  

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.