April 19, 2012

$_GET with Switch Statement

Question by Mor Sela

i have alot of get values that define the page the user gonna see , for example for “profile” i will show the profile page and so on..

to find out what page to display i tried to do something like that:

switch ($_GET) {
    case 'profile':
        require_once('function/profile.php');
        break;
    case 'team':
        require_once('function/team.php');
        break;

but it shows no result..

i send the GET request like that : index.php?profile , for example..

what is the problem here and how can i can manage to do something similar that work as well. thank you in advance!

Answer by stewe

To make your example work, you could replace $_GET with key($_GET)

Answer by Starx

$_GET is a super global variable, where the data are sent as stored as array. So you have to
access it using Index

Assuming you page you are trying to include a page when the data are sent like this:

domain.com?page=product

Then you have to use switch like this

switch($_GET['page']) {
   ....
}

Note: May be I dont have to remind you how vulnerable this code towards injection.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!