Create Batch Job

Is there a way to have a PHP script run periodically in the background?

Not sure if this is the ultimate right solution, but I would like a PHP script to periodically look at the “members” table in my MySQL database and find all Users who have a “logged_in” status of “1” but that also have a “last_activity” value that is at least 4 hours in the past from now().

If any Users that meet these conditions are found, then I want to manually log them out, because the just closed their browsers and didn’t formerly log out.

Is there a way to do that?!

Any suggestion into my larger issue??



Why are you storing the logged in status in the DB in that manner? Is it just to be able to see who’s logged in at a given time?
How are you storing user/ visitor sessions?

As for the batch jobs, you can run PHP in cron jobs. Exactly how this works depends on your hosting. If you’re using cPanel then there’s an option to create cron jobs in there somewhere.

Sorry, it is after 3:00am and I’m exhausted…

My website is heavy into content, and so I have lots of Articles about various topics. Registered Users are allowed to post Comments to each Article.

Each Comment looks very similar to our Posts here on SitePoint and includes the Comment, plus to the left the User’s Username, Online Status, Photo, Location, and # of Posts.

If YOU just finished reading “Postage Meters Can Save You Money” and you were looking at other Users’ Comments, then I need some way to show whether or not each “Commenter” is Online or Offline.

I can’t determine that by looking at each Commenter’s Session, so I have decided to store a piece of information on each Commenter in the “member” table in my database.

I haven’t completely figured things out - obviously - but this is what I dreamed up last night…

  • When a User logs in I set logged_in = 1 and last_activity = now()

  • When a Reader finishes reading an Article and wants to see what others think, the System will display a listing of all Comments pertaining to that Article. For each Commenter, if now() - last_activity < 15 minutes then the Online Indicator is green. If now() - last_activity > 15 minutes but < 30 minutes then the Online Indicator is yellow. And if now() - last_activity > 30 minutes then the Online Indicator is grey and ideally “logged_in” will be reset to “0”.

  • Each time a User does something (e.g. Logs In, Submits a Form, Navigates, etc.) last_activity to is updated to now().

  • If a User closes his/her browser without using the “Log Out” link, then I would have a logged_in = 1 in my database and no way to log out a User.

  • So I thought to myself last night, "For all places where the User needs to be logged in, I could check both logged_in and last_activity. If a User is logged_on but the last_activity is greater than 30 minutes, then I treat them as logged out in my scripts and I find a way to manually go in and update the User record so that logged_in = 0 again. (Thus the reason for this thread.)

Is this approach ideal? No! But I don’t see a better way if I refuse to use Ajax which I am… :cool:

Hope that helps?!

How are you storing user/ visitor sessions?

I create a SESSION for each User when they log in.

As for the batch jobs, you can run PHP in cron jobs. Exactly how this works depends on your hosting. If you’re using cPanel then there’s an option to create cron jobs in there somewhere.

Guess I’ll have to see what you think about what I said above before we discuss this?!



An alternative method:

  • store the sessions in the db, along with the user’s ID
  • when querying to see if the commenters are online, see if they’re in the session table in your db
  • manually delete all session records that haven’t had any activity in a while, or, even better: let PHP’s session handler do it.


  • you never have to worry about users having sessions while they’re not actually logged in
  • users will be ‘logged out’ automatically whenever PHP does garbage collection on sessions
  • you don’t need to use the user table for storing volatile session info (i.e. the logged_in flag)
  • you’ll learn a lot about sessions and PHP’s session handling :wink:

Cons to this approach:

  • Major rewrite of several bits of your code
  • Overriding PHP’s session handling with your own (DB-based) code is tricky if you’ve never done it before and requires a fair amount of insight into how sessions work

Writing a cron job will probably be faster to implement though… :slight_smile:

Is someone volunteering to teach me a new trick?! :wink:

How much work and re-write will be involved?

I am a willing pupil, if you will take me under your wing and help me to understand a better way to do this… :slight_smile: