MrFish Posted September 19, 2010 Share Posted September 19, 2010 I've got a login system that stores all the users information in objects. I serialize the objects and place them in sessions so I can use them through multiple pages. I want to make it so every time the user logs in and the object is constructed the user is added to an "online" table in the database. And every time it deconstructs it will remove that user. I've gotten the construction part to work but it seems that the object deconstructs at the end of the script on every page. But what is weired is that it only constructs once the first time, and even though it shows that is is "deconstructed", it will still hold the same variable values when I enter another page (and desconstruct yet again). So why doesn't my object run the destruct function once the script ends? Is there a way to prevent this or is there any other method to do what I'm trying to do? Link to comment Share on other sites More sharing options...
astralaaron Posted September 19, 2010 Share Posted September 19, 2010 Are you just trying to show who is currently online? Link to comment Share on other sites More sharing options...
Ingolme Posted September 19, 2010 Share Posted September 19, 2010 The class declaration should be present on all the pages where the object is being unserialized. Put it in a separate file and include it in the pages where you need it. Link to comment Share on other sites More sharing options...
MrFish Posted September 19, 2010 Author Share Posted September 19, 2010 I'm doing that but that still doesn't fix it. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 19, 2010 Share Posted September 19, 2010 Try storing the unserialized object in the session instead. I believe you can do that as long as the class is defined before you use session_start, or else it won't get recreated as the right object type. If the object itself is stored in the session, and therefore there would be a reference to it, I don't think it will be destroyed. The destructor only runs once no more references to the object exist. Serializing an object to a string does not cause a reference to exist to the original object. Link to comment Share on other sites More sharing options...
MrFish Posted September 20, 2010 Author Share Posted September 20, 2010 I tried this: $_SESSION[$name] = new Character($id); and this: $_SESSION[$name] = serialize(new Character($id)); and they both cause the same problem. But I also heard that storing objects in sessions was bad practice because it would be heavy on the server. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.