0

I'm creating an admin area for my webpage, and I want to arrange some things in one php file, so I don't have to use different separated files. I have a manage.php file, where I want to put different things depending on how is it called. I'll give an example:

  • If it's called like: manage.php?action=users, it will show me the users in my webpage.
  • If it's called like: manage.php?action=roles, it will show me the roles in my webpage.
  • ....

So, I assume this is done with the $_GET['action'] variable. So this would be the code inside my manage.php file:

<?php
include_once('layout.php');
if ($_GET['action'] === 'users') {
    // The code I want to show...
} elseif ($_GET['action'] === 'roles') {
    // The code I want to show...
} elseif ($_GET['action'] === 'categories') {
    // The code I want to show...
} //etc...
?>

But I think that this is not a good way of doing this because I would have all my code put in different ifs... Is there a way for doing this in a 'cleaner' way? Or is this a good way of doing it?

Thanks!

peregraum
  • 529
  • 1
  • 8
  • 19

7 Answers7

2

IMO, the switch statement is cleaner.

switch ($_GET['action']){

    case 'users'      : getUsers(); break;
    case 'roles'      : getRoles(); break;
    case 'categories' : getCategories(); break;

}

Another approach would be to put all of the actions necessary for showing users, roles, etc, into separate files, and then automatically including that file based on the action. For example:

$controller = $_GET['action'] . '.php';

if (file_exists($controller)) {

    include($controller);

} else {

    // Handle
    echo "invalid request";
}

And then, in a file called users.php,

// do whatever
echo "show all users";
foxygen
  • 1,368
  • 1
  • 11
  • 22
1

You should do it like this:

<?php
include_once('layout.php');

//checks if magic quotes is turned on
if(get_magic_quotes_gpc)
    $action = $_GET['action'];
else
    $action = addslashes($_GET['action']);

switch($action) {
    case 'users':
        // The code I want to show...
        break;
    case 'roles':
        // The code I want to show...
        break;
    case 'categories':
        // The code I want to show...
        break;
} //etc...
?>

it's much cleaner. I also added an addshlashes function

Jonan
  • 2,485
  • 3
  • 24
  • 42
1
<?php
include_once('layout.php');
if ($_GET['action'] === 'users') {
    // The code I want to show...
} else

if ($_GET['action'] === 'roles') {
    // The code I want to show...
} else

if ($_GET['action'] === 'categories') {
    // The code I want to show...
} //etc...
?>
1

Somewhat similar answer to foxygen, if you don't have/want to use a controller approach, put each separate action into a directory (ie, /actions/users.php) and do something like

$action_name = $_GET['action'] . '.php';
$action_path = '/actions/';
$action = $action_path . $action_name;

if (file_exists($action)) {
    include($action);
} else {
    include('/actions/404.php');
}
sqram
  • 7,069
  • 8
  • 48
  • 66
  • 1
    Thanks! I'll use this option! It looks like a good one! BTW, if someone wants to use it, you'll have to edit a like of the code. Instead of `$action_name = $_GET['action'];`, you'll have to put `$action_name = $_GET['action'].'.php';` or it won't work! – peregraum Apr 13 '14 at 15:17
0

You can use switch statement, this will make code more human-readable. Also, if you don't like different files, you can move code in ifs into different functions, i.e. your code will look like:

switch ($_GET['action']){
 case 'users':
  funcUsers(...); // code for users section
 break;
 case 'roles':
  funcRoles(...); // code for roles section
 break;
 case 'categories':
  funcCategories(...); // code for categories section
 break;
}
meta-nickname
  • 83
  • 1
  • 4
0

It's good way when you're using the "if condition", It can be simply extend more condition.

For maintain, code looked clear, it's good for anyone to understand your structure.

Steven Chou
  • 1,504
  • 2
  • 21
  • 43
0

Sanitize get Sanitize $_GET parameters to avoid XSS and other attacks

$action = isset($_GET['action']) ? preg_replace('/[^-a-zA-Z0-9_]/', '', $_GET['action']) : "";
switch ($action){
case 'users': 
/*logic call users*/
break;
case 'roles' :  
/*logic call roles*/
break;
default;
/* logic call index*/

}
Community
  • 1
  • 1