Jump to content


Photo

Indirection to session variable definitions using a session variable


  • Please log in to reply
2 replies to this topic

#1 endicay

endicay
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 July 2006 - 10:31 AM

??? 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( ‘<path_to_application>/application_object.php’ );
session_start();

The problem is that <path_to_application> 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... :-\

#2 endicay

endicay
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 July 2006 - 11:28 AM

>:( 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?

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 12 July 2006 - 04:19 PM

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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users