Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by KevinM1

  1. Looking at it further, your FrontController class should be a Singleton, with setter/getter functions to handle the directory path stuff. I doubt you want to have more than one Front Controller active at the same time. If you're not familiar with that pattern, it's basically: <?php class SingletonExample{ private static $instance; //static, so the class 'remembers' whether or not there's an instance floating around private function __construct(){} //PRIVATE constructor, so it can't be instantiated by the outside world public static function getInstance(){ //checks to see if it's been instantiated yet. If not, make a new one. If so, return the current one if(!self::$instance){ self::$instance = new self(); } return self::$instance; } } ?>
  2. I believe you need to declare $name in your ActionController class. You can't reference $this->name without declaring it first. You also have a small redundancy in your View class. If you define something in an abstract base/super/parent class that you want a child class to have, you don't need to copy it into the child class. As long as those properties and methods are public or protected (recommended), the child class will automatically have them available. That's the whole point to inheritance. So, having another setPageDir() method in your View class is redundant as it's public in the abstract base class.
  3. This question is probably best answered by understanding the very basics of OOP. Classes define the characteristics of objects. They do this by providing (typically) a series of properties (variables) that define the attributes of the object, and methods (functions) that define the the behavior of the object. The class properties depend on what you want the object to represent and do. So, a database object won't have the same properties as a form validation object because they're two wildly different things, with different jobs, despite them both being objects. It all boils down to what you want the objects to do.
  4. 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.
  5. 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.
  6. 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"; } } ?>
  7. Still, the question remains: why are you testing against submit? Is it the actual form submission button? Or have you named another form input 'submit'? If it's the former, I can't think of a reason why you'd need to unset it.
  8. Good point. Regular expressions are really just pattern matching algorithms with unique (funky?) syntax. It's a powerful tool to deal with validating all kinds of input.
  9. It's been a while since I've had to work with a database, so I'd just like to refresh my memory. Data that has been escaped and stored in a database (say, by using mysql_real_escape_string()) will still be output correctly, right? So: <?php $testString = "'This is a quote,' she said"; if(get_magic_quotes_gpc()){ $testString = stripslashes($testString); } $testString = mysql_real_escape_string($testString); $query = "INSERT INTO test_database (test_column) VALUES ('$testString');"; $result = mysql_query($query); $query = "SELECT * FROM test_database"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "{$row['test_column']}"; ?> Is the output: 'This is a quote,' she said OR is it: \'This is a quote,\' she said ?
  10. Yup, as it's the easiest way to do it. Why go through all the trouble if you're not actually processing the card? Unless specifically told to go the extra mile, just ensure that there's 16 digits for the card number.
  11. Actually, it's probably better to go: Check form submission. If it HAS been submitted, do all processing. If data is valid, return the good news to the user (e-mail). If it HAS NOT been submitted OR the data isn't valid, display the form (again).
  12. A few things: 1. In your HTML, the names of your inputs are b[], b1[], etc. Yet, in your PHP, you refer to them all as $_POST['name']. In short, you're reversing the assignment in the PHP. 2. It's clear that you're not comfortable using arrays. Why attempt sticking the names in separate arrays when you need only one array? 3. Again, another array issue. Arrays have the format of: <?php $array[$key] = $value; ?> It looks like you're sticking the key next to the name, rather than inside the brackets. Try something like... HTML: <input type="text" name="b[]" /><br /> <input type="text" name="b[]" /><br /> . . . PHP: <?php $b = $_POST['b[]']; ?> Inserting the values into the DB depends on how your DB is structured.
  13. Since it's for a project, like the last person said, just use regular expressions. There's most likely no need to do more than ensure that the credit card info is in the right format.
  14. More to the point, the following makes no sense: You can't plug in an outside value for use in a foreach-loop. So, either your $a or $nam value isn't what you're hoping it to be. In other words: <?php $qua = $_POST['quantity']; $nam = $_POST['quanname']; foreach($qua as $a => $nam){ /* $a is the key which, in this case, is the number specifying the current value's position in the array $qua[]. $nam is ACTUALLY $qua[$a] here, not $nam[$a], as you're overwriting $nam with each iteration of the loop. */ } ?> Instead of foreach, try a for-loop: <?php for($i = 0; (($i < count($qua)) && ($i < count($nam)); $i++{ //the conditional assumes that $qua and $nam have the same number of values if($nam[$i] > 0){ echo "{$qua[$i]} -- {$nam[$i]}<br />"; } } ?>
  15. KevinM1

    ASP Vs PHP

    From what I understand ASP != ASP.NET. The former did have its own syntax, and seemed somewhat similar to PHP on the surface. I'm not sure if other languages (read: vbscript) could write the old form of ASP. ASP.NET is a framework. It is used by other languages (VB.NET, C#) to do dynamic web application things. From what I've read/heard from some contacts around me, learning C# may actually be the way to go if you want to learn ASP.NET. It seems to be Microsoft's darling, so it should be well supported in the future. I intend to learn ASP.NET as soon as possible, as PHP jobs are few and far between in my area.
  16. This has something to do with my last thread with sessions. I'm currently trying my hand at a Front Controller/Registry combo. Nothing complex, just an exercise for me to learn the basic concepts. I have a Request object, which is primarily an object wrapper for the $_REQUEST superglobal array. The Request object works fine, with the exception being a notice thrown when error_reporting(E_ALL) is set. That notice is: The relevant code is: <?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){ //just in case the command line is used if(strpos($arg, '=')){ list($key, $value) = explode('=', $arg); $this->setProperty($key, $value); } } } function getProperty($key){ if(!empty($this->properties)){ return $this->properties[$key]; // <-- line giving the notice } } function setProperty($key, $value){ $this->properties[$key] = $value; } } ?> The notice is only thrown before a request is sent to the Front Controller (i.e., on initial page load). So, my question is this: does $_REQUEST exist even if a request isn't sent? Because the notice makes it seem as though my code is trying to access the $this->properties array despite it logically being empty.
  17. It was a dumb syntax error. D'oh!
  18. 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: 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?
  19. Hey, sorry for the delay. I think you have too many nested loops. Try: <?php foreach($validators as $validator){ if(!$validator->isValid()){ //if there's an error $errors[] = $validator->fetch(); //fetch the error and add it to the array } //else, do nothing as it's a good result } if(!empty($errors)){ /* do error notification */ } ?>
  20. With error_reporting(E_ALL), I get the following notice on the first run: 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.
  21. 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.
  22. I can't copy the message as there are several code blocks, so I'll have to link to it. Suffice it to say my SessionRegistry doesn't seem to be working. Original message: http://www.phpfreaks.com/forums/index.php/topic,190426.msg854885.html#msg854885
  23. 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.
×
×
  • 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.