note 71457 deleted from control-structures.switch by crell
| From: | crell@php.net | Date: | Mon, 27 Jun 2022 20:36:33 +0000 |
| Subject: | note 71457 deleted from control-structures.switch by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218630@lists.php.net to get a copy of this message | ||
Note Submitter: 2mareks (at) gmail (dot) com
----
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...
}
}
?>