Solved

session_register with included classes

Posted on 2006-11-22
5
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17999209
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
ID: 17999376
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
ID: 17999456
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
ID: 18001408
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
ID: 18001493
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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