Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

session_register with included classes

I want to use session_register() to register classes that are included after the session_start() has been fired.

i.e.:

session_startion()

...some stuff requiring sessions goes here.....

include('class.php');
include('class.php');
include('class.php');
include('class.php');
include('class.php');
include('class.php');
include('class.php');

session_register(class.php)
session_register(class.php)
session_register(class.php)
session_register(class.php)
session_register(class.php)
etc....

How do I do this?
0
DrDamnit
Asked:
DrDamnit
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
http://php.net/session_register
session_register is to register names, not classes.

what are you actually trying to do?
0
 
TeRReFCommented:
What version of PHP are you using?
session_register has been depreciated for quite some time.
If you want to save objects (I guess it's object you want to save right? Not classes, which would make no sense)
then you can just add them to the session like this (you will have to serialize them first if you are using PHP4):

// Just a short example
include('class.php');
session_start();
// suppose there is a class called myClass in class.php
$object = new myClass();
$_SESSION['myObject'] = $object;
// or $SESSION['myObject'] = serialize($object) if PHP4

You can retireve the object the same way:
$object = $_SESSION['myObject'];
// or $object = unserialize($SESSION['myObject']);  if PHP4

Make sure the class.php is included BEFORE you call session_start()
0
 
DrDamnitAuthor Commented:
I have a very complex architecture that runs our internal CRM, asset managent, invoicing, billing, accounts payable, etc....

It is all loaded from a config file. Today we had an error on the main system after installing a new module to it. It took me 3 hours to find the problem, so I have added a ton of debugging information that can be accessed when we load the site in safe mode.

The debugging information uses session variables to store information; however, once the config file and application_top.php files are loaded, it needs to restart the session.

Thus, when I use session variables to store information so they are available for various functions that test the environment, I have to start the session. After the environment is tested, classes are loaded via a load_class() function, which among other things makes sure the class is valid and the file exists. If it is not, then it appends values to an array ($_SESSION['debug_errors']).

So, I have started the session before all the classes are loaded to store debugging information. Then I load the classes, then the applicaiton starts. When the application starts, it doesn't know about the classes that were included after the session had started. THUS.... I need to make those classes available to the session.

I hope that clears things up......but probably not!

Here's the short version.

Program needs session to store info.
Program starts session.
program stores info and is happy.
Program uses include statement to include classes.
Program tries to put class in a session var.
Program not happy.
0
 
hernst42Commented:
You can only unserialize objects in a session if the corresponding class is included BEFORE the session_start() is executed. Else you will loose the class of the serialized/stored object in that session.

From http://de3.php.net/session :
If you do turn on  session.auto_start then you cannot put objects into your sessions since the class definition has to be loaded before starting the session in order to recreate the objects in your session.
0
 
TeRReFCommented:
Like I already mentioned (and hernst42 in more words as well), you have to include class files before session_start(). Your method is not possible because the session has to be started for the includes to be available.

Maybe there is a way around it. You say you need the session to store debug info. Why not store the debug info in a global variable or use a singleton class for this purpose and then start the session and store it in the session AFTER the includes?

Another possibility is to use an external debug tool. A very good one is xdebug:
http://www.xdebug.org/
It will give you tons of information, more than you ever dreamed of :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now