PHP Sessions and custom classes

Posted on 2007-07-29
Last Modified: 2013-12-12
I need to store objects from a class that I wrote in a session, what's the trick?  My class works fine, and I can create an object and store it as a session variable, but I can't use the object's functions  in the session variable.
Question by:natbh

    Author Comment

    Nobody knows?
    LVL 2

    Accepted Solution

    Maybe you should serialize() the object before storing it in the session variable and then unserialize them again when retrieving it from the session.
    LVL 6

    Assisted Solution


    This happens because your class definition is not available when the session data is deserialized.
    You should have the class definition loaded before session_start() is called, so your code should look like:
    require 'path/to/my_class.php';

    Another solution would be to use the "unserialize_callback_func" directive. This is from the php manual(

    unserialize_callback_func directive:  It's possible to set a callback-function which will be called, if an undefined class should be instantiated during unserializing. (to prevent getting an incomplete object "__PHP_Incomplete_Class".) Use your php.ini, ini_set() or .htaccess to define 'unserialize_callback_func'. Everytime an undefined class should be instantiated, it'll be called. To disable this feature just empty this setting.


    Author Comment

    Thank you both for the suggestions, I don't have time at the moment to check to see if they work, but when I do, I'll award the points.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    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 look for a specific file type in a local or remote server directory using PHP.

    794 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

    15 Experts available now in Live!

    Get 1:1 Help Now