I am building an application using PHP. I have a requirement to introduce a locking mechanism across multiple sessions. When one user has locked an entity, any other user sessions should not be able to try updating the same entity. One simple way to implement this lock is to have a lock file (file system). On a first come first serve basis, the first session established gets the lock and after updation, removes the lock. Any other session, if the lock file exists, doesn\'t allow updation.

Is there any other way apart from this approach ? Like in Java/Servlets, we can have a global persistant cache (public static data structure), which can be shared by all servlet objects.

Likewise, is there any mechanism in PHP to create common PHP objects across all HTTP sessions active on a PHP/web server ?

If there is any way, then I can implement a singleton design pattern and that would solve my problem.

Thanks,
Giri

    6 years later

    I have a very similar question.

    I have a memory-intensive function that encodes audio on the server. I would like to write a queuing function that prevents several instances of the encoding object from being instantiated at once -- across all users.

    In other words, only one instance of the audio encoding class should be able to be instantiated at a time, across all active sessions on the server.

    Is this possible without involving a database?

    Anybody?

      Anon that is how I would do it baring one exception.

      You may want to set a time out. Reason being is if one user opens a record and leaves it open till they returns tomorrow morning everyone else may be out of luck till then. Maybe good to let them know their session lock on the record has expired via an alert or something so they can copy out what they were going to change and force them to reopen the file before submit any changes

      But if someone has a better way I am all ears.

        Maybe you could do something with [man]flock[/man] to control access?

        @: just FYI, "anon" is an anonymous user, something that shows up on these forums for posts as old as the original one in this thread, which was posted 6 years ago. 🙂

          boy do I feel dumb someone replied to it and I assumed it was a new post

          LOL

            Write a Reply...