Solved

session_register with included classes

Posted on 2006-11-22
5
244 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]
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 33
phpmyadmin 3 33
Animated .jpg? 13 60
array_values - reorder after unset? 5 12
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

920 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

14 Experts available now in Live!

Get 1:1 Help Now