PHP switch with GET request

17,887

Solution 1

Use $_SERVER['QUERY_STRING'] – that contains the bits after the ?:

switch($_SERVER['QUERY_STRING']) {
    case 'home':
        echo 'admin home';
        break;
}

You can take this method even further and have URLs like this:

http://mysite.com/admin/?users/user/16/

Just use explode() to split the query string into segments, get the first one and pass the rest as arguments for the method:

$args = explode('/', rtrim($_SERVER['QUERY_STRING'], '/'));
$method = array_shift($args);

switch($method) {
    case 'users':
        $user_id = $args[2];

        doSomething($user_id);
        break;
}

This method is popular in many frameworks that employ the MVC pattern. An additional step to get rid of the ? altogether is to use mod_rewrite on Apache servers, but I think that's a bit out of scope for this question.

Solution 2

As well as the ones mentioned, another option would be key($_GET), which would return the first key of the $_GET array which would mean it would work with URLs with other parameters

www.example.com/?home&myvar = 1;

The one issue is that you may want to use reset() on the array first if you have modified the array pointer as key returns the key of the element array pointer is currently pointing to.

Solution 3

$_SERVER['QUERY_STRING']

Solution 4

Is not the most "elegant" way to do it but the simplest form to answer your question is..


    if (isset($_GET['home'])):  
        # show index..  
    elseif (isset($_GET['settings'])):  
        # settings...  
    elseif (isset($_GET['users'])):  
        # user actions..  
    else:  
        # default action or not...  
    endif;

Solution 5

The PHP code:

switch($_GET){
    case !empty($_GET['home']):
       // code here
    break;

    case !empty($_GET['settings']):
         // code here
    break;   

    default:
         // code here
    break;
}
Share:
17,887
Phox
Author by

Phox

Updated on June 04, 2022

Comments

  • Phox
    Phox almost 2 years

    I am building a simple admin area for my site and I want the URLs to look somewhat like this:

    http://mysite.com/admin/?home
    http://mysite.com/admin/?settings
    http://mysite.com/admin/?users
    

    But I am not sure how I would retrieve what page is being requested and then show the required page. I tried this in my switch:

    switch($_GET[])
    {
        case 'home':
            echo 'admin home';
            break;
    }
    

    But I get this error:

    Fatal error: Cannot use [] for reading in C:\path\to\web\directory\admin\index.php on line 40

    Is there any way around this? I want to avoid setting a value to the GET request, like:

    http://mysite.com/admin/?action=home
    

    If you know what I mean. Thanks. :)

  • Sarfraz
    Sarfraz about 14 years
    what if there are more vars other than page vars?
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 14 years
    You don't even need mod_rewrite; $_SERVER['PATH_INFO'] will contain everything after the script in the URL.
  • Tatu Ulmanen
    Tatu Ulmanen about 14 years
    @Ignacio, that's true but then you need the index.php part there.
  • Phox
    Phox about 14 years
    Thanks very much for the help, and I'll probably be using this "MVC pattern" you talked about. :)
  • Yaroslav
    Yaroslav over 11 years
    How does your answer improves previous accepted and upvoted answer? Code only answers are ok, but adding comments to your code increases its usefulness for future readers.
  • Zerquix18
    Zerquix18 almost 10 years
    case true: case false: