how to combinate $session and classes in a php webpage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mv.gonzalez
Forum Newbie
Posts: 3
Joined: Wed Jan 05, 2011 3:31 am

how to combinate $session and classes in a php webpage

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mv.gonzalez
Forum Newbie
Posts: 3
Joined: Wed Jan 05, 2011 3:31 am

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

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post by Jade »

You need to use session_start() at the top of every ajax page you call.
Post Reply