Solved

session_register with included classes

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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