Filter me input

Hi,

I have a very basic script on my web site:


<?php
// start session
// import selected size into session
session_start();
$_SESSION['colour'] = $_GET['c'];
header("Location: " . $_SERVER['HTTP_REFERER']);
?>

The only value I expect from $_GET[‘c’] is either ‘black’,‘white’ or ‘green’.

Is there any way I can filter this so the script will only accept a value from these values and exits if it receives anything different.

Thank you.

<?php
$PossibleColours = array('black', 'white', 'green');
if(array_key_exists('c', $_GET) && in_array($_GET['c'], $PossibleColours)){
    session_start();
    $_SESSION['colour'] = $_GET['c'];
}
header('location: ' . $_SERVER['HTTP_REFERER']);

Brilliant Jake.

Many thanks for the speedy reply. I should’ve known in_array would’ve helped out in some way.

Thanks again.