php random number generator always running on server


Staff member
Quick bit of background - I wanted to build this in .net, but my hosting provider will charge $75 to enable .net, and I'm too tight to pay more. So php it is. I'm not super familiar with php, outside of chopping around in wordpress (which is where I'll probably be implementing this).

What I need is a random number generator (this part is easy), sitting on my server and creating a random number every x seconds, provided there is at least one visitor on the site. So essentially having someone land on the page starts the randomiser, with concurrent visitors all being served the same string of numbers. Does that make sense? Maybe I could set a flag when one visitor arrives, that stops subsequent polling?

I'm thinking that I'd achieve the first part by hooking into load(), starting the service and pumping out the numbers, all pretty straight forward. How do I ensure that concurrent visitors do not poll the service, but are served the output generated by visitor one? I've tried to illustrate what I mean below:

visitor one hits site - polls service 3 5 23 50 29 11 59 84 99 17 34 55
                            visitor two hits site  11 59 84 99 17 34 55
                                              visitor three 99 17 34 55

Hope that makes some sense, any ideas are greatly appreciated...

EDIT: Something like this for a daemon?

class TheDaemon
    public function run()
        while (1) {
            // do stuff here

$daemon = new TheDaemon();