T_5
T_5

Reputation: 31

all variables from url

I have a code like that:

session_start();

$user = $_GET['user'];
$mode = $_GET['mode'];
$id1 = $_GET['id1'];
$id2 = $_GET['id2'];
$id3 = $_GET['id3'];
$id4 = $_GET['id4'];
$id5 = $_GET['id5'];
$id6 = $_GET['id6'];
$id7 = $_GET['id7'];
$id8 = $_GET['id8'];

$dep= $mode;
switch ($dep)
{
    case "3m":
        $dep = "Text 1";
        break;
    case "all":
        $dep = "More text";
        break;
    default:
        $dep = "Text1";
}

There are more other cases. I think I will have more id's and cases soon. Is there a simpler way to get all id's from URL push them into PHP code for evaluating?

I have found a code foreach:

    foreach($_GET as $key => $value) {
        echo  'key is: <b>' . $key . '</b>, value is: <b>' .$value. '</b><br />';
    }

And it gets all variables from URL, but how to change the code in order to have it like this:

$variable = $_GET['variable'];

Any help appreciated :)

Upvotes: 0

Views: 156

Answers (4)

eis
eis

Reputation: 53462

Assuming I understood correctly and you want all GET variables to be actual variables you can use, there's a function extract to do that.

However, as noted in the documentation:

Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting flags values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

So basically you should not do this unless you have good reasons, and be careful if you do as to not overwrite any existing values.

However, if your alternative is to do this:

foreach($_GET as $key => $value) {
        $$key=$value;
}

then extract is certainly better, as a) you can set it to not overwrite any existing variables, and b) you can have a prefix, so those imported variables can be distinguished. Like this:

extract ( $_GET, EXTR_SKIP);

For $user, $mode, $id which don't overwrite anything, or

extract ( $_GET, EXTR_PREFIX_ALL, 'user_input' );

for $user_input_mode, $user_input_user, $user_input_id etc.

Upvotes: 1

deceze
deceze

Reputation: 522101

Use arrays, that's exactly what they're for. Name the parameters in the URL like:

id[]=1&id[]=2&...

Then $_GET['id'] is an array which you can easily loop through.

Many ids means you're looking for an array of ids, not id1, id2, id3 etc. An array allows you to access them virtually the same way as $id[1], $id[2] etc, but without the headache of needing to herd hundreds of independent variables.

There's also no need to assign each value from $_GET into its own variable, you can use those values directly from $_GET. They're not getting any better by assigning them into individual variables.

Upvotes: 2

You can write

foreach($_GET as $key => $value) {
        $$key=$value;
    }

this will assign every key name as a varibale.

Upvotes: -1

Ryan
Ryan

Reputation: 14649

function getIDs()
{ 
    $ids = array();

    if(isset($_GET['id']))
    {
       foreach($_GET['id'] as $key => $value)
       {
          $ids[$key] = $value;
       }
    }
   return $ids;
}

If you can get your URL to send an array of id GET parameters to a PHP script you can just parse the $_GET['id'] array in the above function, assuming the URI looks like ?id[]=1&id[]=2&id[]=3

$ids = getIDs();

Upvotes: 0

Related Questions