Forbes,
Thanks for the puzzle. Nothing like a good Gordian knot to keep one tossing and turning all night.
From your code snippet, it seems that you've wrapped the calls to various classes inside of a switch statement. Each time the page is called, the proper code segment runs depending upon the state of the user (logging in, logging out, invalid, etc.).
I toyed with the idea of doing that, but was looking for a solution that didn't require building some sort of if or switch statement. What I want to achieve is for any user to simply make a new secureLog object, use the methods and be done.
Now, your code may well do that. One thought I had (and you may be doing this as evidenced by the double -> operator) was to wrap the classes inside of other classes. Your switch statement may be inside of a class. This way, I'm still using OOP. But even with this method (and assuming I don't use if or switch statements as part of the page structure), I still can't see a way around creating an orphan or making a new object when the page is called again by the form post.
On experts-exchange, I got a response to my query that I think resolves my issue. I'm not sure I understand exactly how it is working, but I'll tell you what I think is happening, then post the code.
The main page calls a method of the object outside of the class. This function does some testing, which if fails, then instantiates a secureLog object. It probably violates OOP practices, but it does seem to accomplish what I'm driving at: no control structures outside of the class to implement the functionality I want. Here's the code. I'm interested in what you think of it.
PHP Code:
<?php //index.php
require_once('secureLog.php');
secureLog::check('New', 'Test Page', 'table.php','Enter user id..etc');
?>
<?php //secureLog.php
class secureLog
{
/*static*/ function check($constructor_var1, $constructor_var2, $form_var1, $form_var2)
{
if(authenticated)
return;
if($_SERVER['REQUEST_METHOD'] == 'POST' &&
!empty($_POST['username_field_name']) &&
!empty($_POST['password_field_name']))
if(secureLog::testAndAuthenticate($_POST['username_field_name'], $_POST['password_field_name']))
return;
$mast = new secureLog($constructor_var1, $constructor_var2)
$mast->setMastHead();
$mast->makeForm($form_var1, $form_var2);
$mast->makeFoot();
exit();
}
/*static*/ function testAndAuthenticate($user, $pass)
{
.. enter some db testing here ..
return $user_authenticated; //true when user logged in succesfully, false on failure.
}
..rest of your class
}
thanks for your time.
Bookmarks