Jump to content

Recommended Posts

Data (including objects) are autmatically serialized when placed into the $_SESSION array, so no, no special preprocessing needs to occur.

 

Be aware though that resources cannot be serialized, hence if your objects contain any, they will break.

 

Thanks.  Hmmm...now to figure out why my session variable isn't being set.

I didn't want to bump this up, but the time limit on editing my last post expired.

 

I'm trying some simple Front Controller/Session Registry stuff, just to see if I can get it working.  The Controller works great, but, for some reason, my Session Registry isn't.  Since there are several classes in play, this might be a long-ish post.

 

First, the Controller itself:

<?php
   class MP_controller_Controller{
      private function __construct(){}

      static function run(){
         $instance = new MP_controller_Controller();
         $instance->init();
         $instance->handleRequest();
      }

      private function init(){
         $sessReg = MP_base_SessionRegistry::getInstance();

         if($user = $sessReg->getUser()){ // could this be the culprit?
            echo "Welcome back, {$user->getName()}<br /><br />\n\n";
            echo "Session dump: {$_SESSION['user']}<br /><br />\n\n";
         }
      }

      private function handleRequest(){
         $request = new MP_controller_Request();
         $commandFactory = new MP_command_CommandFactory();
         $command = $commandFactory->getCommand($request);
         $command->execute($request);
      }
   }
?>

 

It's pretty straight forward.  Upon page load, it initializes, which checks the Session Registry for a User object.  If it exits, simply dump the name to the screen.  I added an extra debugging output line.  After initialization, it processes whatever request was given to it by checking that request's action.

 

Now, my Request object, which is used heavily:

<?php
   class MP_controller_Request{
      private $properties;

      function __construct(){
         $this->init();
         MP_base_RequestRegistry::getInstance();
         MP_base_RequestRegistry::setRequest($this);
      }

      function init(){
         if($_SERVER['REQUEST_METHOD']){
            $this->properties = $_REQUEST;
            return;
         }

         foreach($_SERVER['argv'] as $arg){
            if(strpos($arg, '=')){
               list($key, $value) = explode('=', $arg);
               $this->setProperty($key, $value);
            }
         }
      }

      function getProperty($key){
         return $this->properties[$key];
      }

      function setProperty($key, $value){
         $this->properties[$key] = $value;
      }
   }
?>

 

SessionRegistry:

<?php
   class MP_base_SessionRegistry extends MP_base_Registry{
      private static $instance;

      private final function __construct(){
         session_start();
      }

      static function getInstance(){
         if(!self::$instance){
            self::$instance = new self();
         }

         return self::$instance;
      }

      protected function get($key){
         return $_SESSION[__CLASS__][$key];
      }

      protected function set($key, $value){
         $_SESSION[__CLASS__][$key] = $value;
      }

      public function getUser(){
         return self::$instance->get('user');
      }

      public function setUser(MP_base_User $user){
         self::$instance->set('user', $user);
      }
   }
?>

 

Again, pretty simple.  Am I right in thinking that the base get/set functions create a 2D array that's basically in the form of $_SESSION['SessionRegistry']['user']?  I doubt that's my problem, but I'd still like to double check.

 

The Command object (in this case, LoginCommand) starts a chain of function calls, pushing the Request object down the line:

<?php
   class MP_command_LoginCommand extends MP_command_Command{
      public function doExecute(MP_controller_Request $request){
         $loginRec = MP_base_ReceiverFactory::getLoginReceiver();
         $loginRec->login($request);
      }
   }
?>

 

ReceiverFactory isn't really important, but I'll include it to illustrate the function calls:

<?php
   class MP_base_ReceiverFactory{
      private final function __construct(){}

      public static function getLoginReceiver(){
         return new MP_base_receiver_LoginReceiver();
      }
   }
?>

 

The LoginReceiver (yes, I write notes to myself in my code while prototyping):

<?php
   class MP_base_receiver_LoginReceiver{
      public function login(MP_controller_Request $request){
         /* in a real-world use, I should check $request info against
         database values, returning a user only when everything is
         valid */

         $user = new MP_base_User($request);
         $sessReg = MP_base_SessionRegistry::getInstance();
         $sessReg->setUser($user);
         return $user;
      }
   }
?>

 

And, the User class to round things out:

<?php
   class MP_base_User{
      private $userName;
      private $password;

      public function __construct(MP_controller_Request $request){
         if(($user = $request->getProperty('user')) && ($pass = $request->getProperty('pass'))){ //could the problem be here?
            $userName = $user;
            $password = $pass;
         }
      }

      public function getName(){
         return $this->userName;
      }
   }
?>

 

Lots of objects, but pretty simple process.  I just can't figure out why my session isn't working.

Where you specified yourself the following line "should" be at fault.

if($user = $sessReg->getUser()){ // could this be the culprit?

...

 

If i'm not mistaken on your first run this WON'T return a user object because one doesn't exist. I believe I had the same problem.

Make sure you have error_reporting turned onto a high level and you should see some warnings/errors.

 

note: I also debated whether to use a static function for getUser() so you can call MP_base_SessionRegistry::getUser();

Where you specified yourself the following line "should" be at fault.

if($user = $sessReg->getUser()){ // could this be the culprit?

...

 

If i'm not mistaken on your first run this WON'T return a user object because one doesn't exist. I believe I had the same problem.

Make sure you have error_reporting turned onto a high level and you should see some warnings/errors.

 

note: I also debated whether to use a static function for getUser() so you can call MP_base_SessionRegistry::getUser();

 

I don't think that line is actually the problem, though, as it seems to work right on the first run, as it doesn't even attempt to output the user.  On a subsequent pass, it gets something, as it tries outputting the user info, but it just doesn't output anything useful.  Instead, it just says "Welcome back,    ."

 

I'll play with error_reporting to see if that will tell me anything.

With error_reporting(E_ALL), I get the following notice on the first run:

Notice: Undefined index: action in /home/nights/www/www/MP/controller/Request.php5 on line 27

 

I'm not sure how to remove it, as it seems to be ignoring whatever check I throw at it:

<?php
.
.
.
      function getProperty($key){
         if(!empty($this->properties)){
            return $this->properties[$key];
         }
      }
.
.
.
?>

 

The notice disappears when I actually try my login/session test.

 

It's gotta be a logic error I'm not seeing, as sessions are enabled.

Sorry to bump this up, but I'm still having the same session problems, and I'm not sure how to fix it.  I get two warnings:

 

Notice: Undefined index: action in /home/nights/www/www/MP/base/SessionRegistry.php5 on line 18

Notice: Undefined index: action in /home/nights/www/www/MP/controller/Request.php5 on line 27

 

The lines in question --

 

SessionRegistry:

<?php
.
.
.
      protected function get($key){
         return $_SESSION[__CLASS__][$key]; // <-- This line
      }
.
.
.
?>

 

Request:

<?php
   class MP_controller_Request{
      private $properties;
.
.
.
      function getProperty($key){
         if(!empty($this->properties)){
            return $this->properties[$key]; // <-- This line
         }
      }
.
.
.
?>

 

Any ideas?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.