note 71457 added to control-structures.switch
| From: | dot) | Date: | Wed, 29 Nov 2006 23:03:46 +0000 |
| Subject: | note 71457 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120171@lists.php.net to get a copy of this message | ||
In reply to earlier comment, "switch"- I found this to be one of the best ways to
interpret 'actions'. Simply create a new instance of Handler_action before including any
content source files. This is a highly stripped version of the class.
The real one I created handles (and secures) input for $_GET and $_POST, creates a
'permission' array that only allows certain actions to be called by non-admins, and
creates handy little diagnostic messages that can be displayed upon redirecting.
On that note, the beauty in this class really shines in the simple redirect. You wont be left with
ugly URLs like, "http://www.domain.com/path/to/script.php?action=blah&var1=123".
Rather, you will be left with something like "http://www.domain.com/path/to/script.php"-
helps protect some of the site by not showing any vulnerabilities in URLs.
Also, this class keeps all actions organized neatly by directly passing $_GET vars to the actions
through function parameters.
<?php
class Handler_action {
function __construct( ){
//Add code here to secure attacks through $_GET or use $_POST
$action = $_GET["action"];
//$actions_index conventions:
//'action_name' => array( 'arg1', 'arg2', 'etc',
['/redirect/to/path' | NULL ] )
$actions_index = array(
'create' => array( $_GET['newVar1'], $_GET['newVar2'],
'/home.php' ),
'edit' => array( $_GET['id'], $_GET['otherVar'],
'/home.php' ),
'delete' => array( $_GET['id'], '/other_script.php' )
);
if( $action && array_key_exists( $action, $actions_index ) ){
$redirect_path = array_pop( $actions_index[$action] );
call_user_func_array( array( &$this, $action ), $actions_index[$action] );
if( $redirect_path )
header( "Location: http://www.domain.com{$redirect_path}"
);
}
}
//being defining actions now
function create( $new_var1, $new_var2 ){
//code...
}
function edit( $id, $other_var ){
//code...
}
function delete( $id ){
//code...
}
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 24.94.237.207
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/71457
Del: integrated -- https://master.php.net/note/delete/71457/integrated
Del: useless -- https://master.php.net/note/delete/71457/useless
Del: bad code -- https://master.php.net/note/delete/71457/bad+code
Del: spam -- https://master.php.net/note/delete/71457/spam
Del: non-english -- https://master.php.net/note/delete/71457/non-english
Del: in docs -- https://master.php.net/note/delete/71457/in+docs
Del: other reasons-- https://master.php.net/note/delete/71457
Reject -- https://master.php.net/note/reject/71457
Search -- https://master.php.net/manage/user-notes.php