Page 1 of 1

how to combinate $session and classes in a php webpage

Posted: Wed Jan 05, 2011 4:29 am
by mv.gonzalez
Hello,

I amb developing a web in php with xajax, and I've got some classes.

My idea is the following: when a person logs in the webpage, it creates a session and I fill and object with the characteristics of the user (this object is from a class).

The problem I have is that $session can't save ans instance of an object (or I don't know how this can be done). I mean, it's possible to do "$session["myVar"]=new myClass()" ? An thats why everytime the user goes from one side to another of the web, I have to instance the object again and again, and I think that this it is not necessary cause the characteristics of the user doesn't change.

If it's not I don't know how can I make a modular webpage if sessions and classes are not compatible. Can someone help me?

Thanks in advance

Re: how to combinate $session and classes in a php webpage

Posted: Wed Jan 05, 2011 11:19 am
by Jade
Make sure you're including your class files before you start the session. If your class files aren't included prior to session_start() you'll get invalid instance reference errors.

Re: how to combinate $session and classes in a php webpage

Posted: Wed Jan 05, 2011 12:41 pm
by AbraCadaver
Also, check to see if it is in the session:

Code: Select all

include('myclass.php');
session_start();

if(!isset($_SESSION['myVar'])) {
   $_SESSION['myVar'] = new myClass;
}

Re: how to combinate $session and classes in a php webpage

Posted: Fri Jan 07, 2011 1:15 am
by mv.gonzalez
Ok Thanks. I've done that and it works, but when the value is lost when an event occurs.

I'll explain:

I have a webpage with a lot of divs, and when a button is pressed or a form is completed, etc. An xajax function changes the content of the divs, and if I look for the value of $_SESSION["myVar"] it ios empty, and if I use the isset function, it cretes it again.

Why is it disapearing? Can you help me?

Thanks.

Re: how to combinate $session and classes in a php webpage

Posted: Fri Jan 07, 2011 9:38 am
by Jade
You need to use session_start() at the top of every ajax page you call.