Jump to content

Variable can only have certain values


dadamssg

Recommended Posts

how do i wrote a streamline code to make a $_GET['value'] only have certain values. For instance if $_GET['value'] is NOT equal to either 'music', 'sports','random','whatever' send user elsewhere?

 

I'm going to use this value in a form as a default for a selection list. And i wanna make sure the variable is listed as an option.

Link to comment
https://forums.phpfreaks.com/topic/168872-variable-can-only-have-certain-values/
Share on other sites

people can just type things in the URL to make it whatever they want so you have to work from it after that...

 

$value = $_GET['value'];

if ($value != "music" && $value != "sports" ...etc...)

  $value = "music";

//or whatever you want it to equal if it is not valid

If your variable can only contain certain values then setup an array of possible values.

$accepted_values = array('value1', 'value2', 'value3', etc);

// now see if $_GET['value'] contains a value that is accepted
if(in_array($_GET['value'], $accepted_values))
{
    // $_GET['value'] holds an accepted value
}

 

You can ofcourse go the other way and setup an array of non accepted values

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.