Storing an object as a session variable.

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Storing an object as a session variable.

Post by Luke »

Hello, I am new to this forum, sorry to make this my first post... I will be helpful... I promise. Anyway, here's my problem. I am making a program where my town's chamber of commerce can log in, add, delete, and edit posts of events on their website. All was going fine until I tried to store an object in a session and then use it later on. Here's the script:

Code: Select all

include_once(".../includes/basicfunctions.php");
include_once(".../includes/sessionfunctions.php");
include_once(".../includes/validationclasses.php");

if($_POST['action'] == "preview"){
    $event = new event($_POST);
    if($preview = $event->view()){
        session_register('post');
        $_SESSION['post'] = $event;
        $msg = "Submit Post?";
        $preview = urlencode($preview);
    }
    else{
        $msg = "Could not post. Please contact administrator.";
        logerror("Could not preview post. event->preview()");
    }
}
else{
    $event = $_SESSION['post'];
    if($event->post()){
        $msg = "Post successful";
    }
    else{
        $msg = "Could not post. Please contact administrator. ";
        logerror("Could not post. event->post()");
    }
}
header("Location: $rootdir/edit/calendarevents.php?msg=$msg&preview=$preview");
and here is the error I am getting...

Code: Select all

Fatal error: main() [function.main]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "event" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in c:\wamp\www\postprogram\forms\postcalendar.php on line 21
Does anybody know how I can store an object in a session and use it later?


feyd | please use

Code: Select all

tags when posting php code.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

include the class definition before you call session_start()
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Wow... that was too easy. Thank you very much.... all I had to do was switch the two includes on the top.
Post Reply