Jump to content

Archived

This topic is now archived and is closed to further replies.

endicay

Indirection to session variable definitions using a session variable

Recommended Posts

??? I have solved a problem but I’m not sure that I understand the solution.  Does anyone have any ideas and is the solution likely to give me trouble later?

Here is the context of the problem.  I have developed a generalised framework for web sites based on MySQL.  This comprises a set of core modules to be used by all applications of the framework.  Each application creates an object with its definitions and stores it in the session ($_SESSION) allowing the core modules to discover which application is using them and its location on the server.  An application may create other objects and store them in the session.  The problem arises when a core module needs to make use of such an application object stored in the session.

The PHP documentation makes clear that objects must be defined before starting the session.  Hence, in normal circumstances,

require_once( ‘some_class.php’ );
session_start();

For a core module that uses an application object, I need to be able to say

require_once( ‘some_class.php’ );
require_once( ‘application_definitions_class.php’ );
require_once( ‘[color=red]<path_to_application>[/color]/application_object.php’ );
session_start();

The problem is that [color=red]<path_to_application>[/color] is contained in a session object defined by ‘application_definitions_class.php’.  I can’t read the path from that object until the session has started and I can’t use the path until I have read the object from the session.

My eventual, apparently working, solution, after trawling through the online documentation and various annotations by users, is this.

require_once( ‘some_class.php’ );
require_once( ‘application_definitions_class.php’ );
session_start();
$path = $_SESSION[ ‘application’ ]->app; // a property of an instance of  the class in ‘application_definitions_class.php’
session_write_close();
require_once( $path . ‘/application_object.php’ );
@session_start();

The PHP documentation states, “As of PHP 4.3.3, calling session_start() while the session has already been started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.”  However, the second call makes a very definite difference inasmuch as my application works only if I include it.  If I leave the @ off the second call to session_start() I get “Notice: Constant sid already defined in <whatever>.php on line <x>” suggesting that session_write_close() has not completely finished off the first session and there may be trouble ahead... :-\

Share this post


Link to post
Share on other sites
>:( Forget it!  I've just realised I'm using 4.2.1.  My solution won't work on later version, especially when I move it to 5.1.1 on our new server.    Does anyone know how to do what I want to do?

Share this post


Link to post
Share on other sites
You can put session_start() at the very start of the file, session_start() doesn't rely on anything else as far as I know.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.