KevinM1 Posted April 11, 2008 Share Posted April 11, 2008 I'm still playing around with my Front Controller and Registry test cases. During my tweaking, I noticed that there was a bit of a hiccup with things being set in/retrieved from sessions. My test case basically does this: If user info is in session, output user name to screen -> regardless, if a login action was specified, login that user using their info. The problem is that when the page refreshes, it still shows the old user's name, if there was an old user, or no name at all if it's the first try. It displays the correct name if I login using the same test info, but never on the first try. My example can be found at http://www.nightslyr.com/controllertest.php5 There's a whole bunch of code to post, so bear with me please. What I put below is what seems to be relevant to this issue. controllertest.php5 (config.php5 is just my autoloader): <?php require_once("data/config.php5"); error_reporting(E_ALL); MP_controller_Controller::run(); ?> <html><body> <a href="controllertest.php5?action=test&name=Kevin&email=blank">TestCommand test</a><br /><br /> <a href="controllertest.php5?action=login&user=Kevin&pass=1234">Login/session test</a><br /><br /> <a href="controllertest.php5?action=login&user=Bubba&pass=5678">Login/session test 2</a> </body></html> MP_controller_Controller: <?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(!$sessReg->isEmpty()){ $user = $sessReg->getUser(); if($user){ echo "Welcome back, {$user->getName()}<br /><br />\n\n"; } } } private function handleRequest(){ $request = new MP_controller_Request(); $commandFactory = new MP_command_CommandFactory(); $command = $commandFactory->getCommand($request); $command->execute($request); } } ?> MP_controller_Request: <?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){ if(is_array($this->properties) && isset($this->properties[$key])){ return $this->properties[$key]; } } function setProperty($key, $value){ $this->properties[$key] = $value; } } ?> MP_base_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 isEmpty(){ if(!isset($_SESSION[__CLASS__])){ return true; } else{ return false; } } public function getUser(){ return self::$instance->get('user'); } public function setUser(MP_base_User $user){ self::$instance->set('user', $user); } } ?> MP_command_LoginCommand: <?php class MP_command_LoginCommand extends MP_command_Command{ public function doExecute(MP_controller_Request $request){ $loginRec = MP_base_ReceiverFactory::getLoginReceiver(); $loginRec->login($request); } } ?> MP_base_receiver_LoginReceiver (yes, I write notes to myself): <?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; } } ?> MP_base_User (__toString() was used for debugging purposes): <?php class MP_base_User{ private $userName; private $password; public function __construct(MP_controller_Request $request){ $user = $request->getProperty('user'); $pass = $request->getProperty('pass'); if($user && $pass){ $this->userName = $user; $this->password = $pass; } } public function getName(){ return $this->userName; } public function __toString(){ return "<br />User Name: {$this->userName}<br />\nPassword: {$this->password}<br /><br />\n\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100674-solved-session-registry-hiccup/ Share on other sites More sharing options...
KevinM1 Posted April 14, 2008 Author Share Posted April 14, 2008 I still can't find the source of the session hiccup/delay. I've tried a few small-scale session tests, and they all work automatically. Something, somewhere, is delaying the session in my registry from being set when it should be, but I can't see it. From what I can see, it should be working properly. Do any of the OOP experts have any ideas? I'm stumped. Quote Link to comment https://forums.phpfreaks.com/topic/100674-solved-session-registry-hiccup/#findComment-516701 Share on other sites More sharing options...
KevinM1 Posted April 15, 2008 Author Share Posted April 15, 2008 I figured it out. It was a timing issue between init() and handleRequest(); Since init() was always called before handleRequest(), it was always accessing the old info (in other words, --($_SESSION[__CLASS__]['user'])). Adding a separate display() function to the controller fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/100674-solved-session-registry-hiccup/#findComment-517890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.