Jump to content

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... :-\
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.