Take the 2-minute tour ×

I'm building a webapp. HTML+AJAX <--> PHP <--> MySQL; very traditional. What I need to do is have some state that all sessions can read from, and something that drives state changes even when there are no users looking at the site.

A simple example would be a counter. We start the counter running from zero, and then whenever any user connects to the website, they see the current value of the counter. Even if nobody is looking at the website, the counter still increases. Everyone looking at the website sees the same value at the same time.

What's the best way to implement this? It seems like I should be able to do this without resorting to storing values in the database and having everyone query the database for state. But I don't know how to have PHP running when nobody is connected. Do I need a fourth component? I don't have my own webserver, this stuff is all hosted at some off the shelf place that setup the environment for me, but they seem to have most up-to-date and reasonable stuff one would expect in this sort of stack.

Clearly this isn't my area of expertise, so any nudges in the right direction are appreciated.

share|improve this question
If you need a shared state between sessions, you'll need permanent(ish) storage like a database or if that's overkill you could use flat files. PHP sessions are supposed to be independent. –  NullUserException Sep 21 '10 at 1:57
add comment

3 Answers

up vote 1 down vote accepted

You could set a global PHP variable and if your web host allows it in your control panel set up a cron job to run a php script in your app that increments the variable every certain amount of minutes.


Usually web hosts will have a cron scheduler app in their control panel that lets you pick what script to run and at what time intervals.

share|improve this answer
For what I want to do a PHP global is probably sufficient. I'll look into cron access. Thanks. –  jeffamaphone Sep 21 '10 at 1:59
add comment

Could you explain what exactly you want to do in the background though? Whatever the case, it sounds like you're going to want to look into setting up a cron task to execute a console based php script to perform this additional functionality. You can schedule cron tasks to execute however frequently you wish. Setting up cron tasks can be done from any control panel worth its salt (cPanel, Plesk, Exim, whatever).

As far as actually keeping this information available to all sessions in your main front-facing PHP code, you could store the information in a Cache (using Memcache)

share|improve this answer
Well, the timer example is a very simplified form of what I want to do. Basically I have a timeline of things that happen, and I want everyone who views the site to see the site in the same state along the timeline. It's not a lot of information, just an index really, so Memcache is probably overkill. –  jeffamaphone Sep 21 '10 at 2:00
You'd be surprised how easy it is to get a Memcache daemon up and running and how simple it is to use the API. –  Sam Day Sep 21 '10 at 2:12
Well, I'll have a look at it and see. Thanks. –  jeffamaphone Sep 21 '10 at 16:23
add comment

What makes the counter increase?

You could use a cron job.

To have the state persist over all connections, it will either need to be stored in a database or as a file (which a database really is anyway).

share|improve this answer
I'm not sure, which was why I was asking. Seems like a cron job is the way to go. Thanks. The various states it can be in will be in a table in the DB, but the index into the current state will be frequently changing. –  jeffamaphone Sep 21 '10 at 2:02
@jeffamaphone Good luck and happy coding! –  alex Sep 21 '10 at 7:12
add comment

Your Answer


