Simulating a Session_OnEnd event for handling?

Please excuse me. I am a .NET developer, however, I am working on a small project for which the requirements call for PHP. Therefore, I have a few questions. I will post the first here and see where this goes before I move on.

First, I fully realize that Apache and PHP do not always work the same way as IIS and .NET, but what those differences are eludes me as I do not have the time for proper research. It is my hope that the kind PHP folk here already have insights to share.

In a .NET MVC application we have the concept of an Application class, which contains event handling methods for various things. An application is loaded once, when it is first called via the web, and disposed of when IIS recycles it’s application pool. This may be long after all users are done with it. It is able to do this because IIS send a notification to the application that it is about to close. The same mechanism happens when a session or request begins or ends, and many other things.

Apache isn’t a managed system however (to my knowledge) and I have to rely on PHP code only to duplicate some of this functionality. I am aware of the session_set_save_handler, but am unsure as exactly how to use it, despite an example being on the manual page.

In .NET MVC, when using dependency injection, one can specify the lifestyle scope of a dynamically constructed element, using caching mechanisms such as RequestScope() or SessionScope(). Now, I can create these classes and have them maintain a component instance in either the $_REQUEST or $_SESSION arrays, but there is a problem. I need to know when a session actually ends (I’m not asking how to end it) in order to destroy certain objects, calling their destructors. Handling request begin and end is easy, fire them off at the top and bottom of the script execution, handling session begin is easy (if session_id is null, start it and call the session start handler), but responding to session end eludes me.

Is there a way to do this, or do I have to tell the project manager that he simply cannot have what he wants.

Example…


class MyApp : MvcApplication
{

    public function BeginSession() { // set up dependency injection once, here, instead of on each request }

    public function BeginRequest() { // typical ob_start crud and other volitile things }

    public function EndRequest() { // typical output formatting and handling}

    public function EndSession() { // tear down any cached objects such that their destructors are called }

}

A session ends when the script ends. Always. But the data of the session remains and starts again with that data when the user enters a new page or refreshes. Assuming I understand what is it you are asking…In PHP everything ends when the script does, all handles and what not are closed as well.

If you want to have more control over how things end, you can use register_shutdown_function.

Thanks for the info. After some independent research, I’ve found that what I want to do isn’t even really possible with PHP. There are some resource intensive things I wanted to do just once instead of at the start of each script. In ASP.NET, I can do this because there’s another level above request and session, and that’s the application. Variables stored there are accessible to every client. They are initialized when the application (on the server) is started, and terminated when it ends. As far as I can tell, nothing like this exists in PHP. The closest I can come, is to monitor the /tmp folder where the sessions are stored by default. On script start, if no sessions are there, initialize stuff, and at the end of the script, if no sessions are there, destroy things. The problem would be in making something shared between threads. Singletons are useless, as you probably already know, for this. So I am not sure where to head. Most likely, the only choice is convincing the client that PHP simple isn’t going to meet our needs.

I believe what you mean to say is that PHP is not going to meet YOUR needs. Does the client need PHP to emulate how ASP.NET works?

Why do I get the impression you’re being a little sensative? I hope I’m wrong. The term ‘our’ meant this particular effort between client and myself. And yes, for several things: proper function overloading with typed params (scalars included), multiple declarations for object constructors, and the ability to provide cached services across multiple Http clients via managed code. I tried, but I just can not duplicate what we need. Not that it’s your concern, just letting you know where we were coming from. Costs are beginning to overwhelm, and I was asked to see if their current presence could be recoded effectively with PHP. Sure, rewriting is possible, but it would involve too much in terms of actually changing how the programs operate. It can’t be a straight port. So I have to deliver some news that might not go over so well. The choice as I see it is: stick with asp.net and shell out the bucks, or start from scratch and completely rewrite the apps.

Please note: this one instance has no bearing on my personal opinion of PHP. I’ve used it quite frequently over the years.

There is no method of being notified when a sessions ends. You can (probably) however perform some action in the session garbage collector.

http://www.php.net/manual/en/class.sessionhandlerinterface.php

http://www.php.net/manual/en/sessionhandlerinterface.gc.php