Solved

session_register with included classes

Posted on 2006-11-22
5
243 Views
Last Modified: 2013-11-18
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
Comment
Question by:DrDamnit
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
http://php.net/session_register
session_register is to register names, not classes.

what are you actually trying to do?
0
 
LVL 29

Expert Comment

by:TeRReF
Comment Utility
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
 
LVL 32

Author Comment

by:DrDamnit
Comment Utility
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
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
Comment Utility
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
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 250 total points
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now