Page 1 of 1

Array's and Conditional's

Posted: Mon Aug 08, 2005 9:24 am
by theda
I decided "Hmm, let's get rid of this little hole in my script!" And what is that hole mind you? Conditional isset functions!

Example:
http://www.example.com/page.php?variable=notsomething
Choices are either something or somethingelse... You type in notsomething... But the script should default to something if variable doesn't equal either something or somethingelse.

My feeble attempt didn't go as planned... I'm missing something.

Code: Select all

// Conditional Setcookie - Theme
	if (isset($HTTP_COOKIE_VARS['the']) AND empty($HTTP_GET_VARS['the'])) {
		$the = $HTTP_COOKIE_VARS['the'];
	} elseif (isset($HTTP_GET_VARS['the'])) {
		$arr = array('erd','gra','ora','fli','low','ros','see');
		if($the == $arr) {
			$the = $HTTP_GET_VARS['the'];
			setcookie('the',$the, time()+60*60*24*30);
		} else {
			setcookie('the','gra', time()+60*60*24*30);
			$the = 'gra';
		}
	} else {
		setcookie('the','gra', time()+60*60*24*30);
		$the = 'gra';
	}

Posted: Mon Aug 08, 2005 10:02 am
by s.dot

Code: Select all

if(($_GET['variable'] != "something") || ($_GET['variable'] != "somethingelse")
{
   die("invalied $_GET variable");
}
Or perhaps you're checking to see if $the is in the array

Code: Select all

if(in_array($the,$array)
{
  // proceed
} ELSE
{
   die();
}

Re: Array's and Conditional's

Posted: Mon Aug 08, 2005 10:10 am
by nielsene
I think I'd recode this to:

Code: Select all

// Conditional Setcookie - Theme
if (isset($_COOKIE['the']) && !isset($_GET['the'])) {
    $the = $_COOKIE['the'];
} elseif (isset($_GET['the'])) {
    $arr = array('erd','gra','ora','fli','low','ros','see');
    if (in_array($the,$arr) {
        $the = $_GET['the'];
        setcookie('the',$the, time()+60*60*24*30);
    } else {
        setcookie('the','gra', time()+60*60*24*30);
        $the = 'gra';
    }
} else {
    setcookie('the','gra', time()+60*60*24*30);
    $the = 'gra';
}
Notice the two "real" changes-- the first call to empty was changed to a !isset and the in_array test in the deepest if statement

Posted: Mon Aug 08, 2005 4:47 pm
by theda
And you harrassed my deprecated Globals! Lol...