0

I made a redirect script as listed below... no matter what value of p is passed, even if valid, it is redirected to the default.htm page... essentially it is skipping the if/else part and always going to the else.

Is there something special about the php header function where it is disregarding the conditional statements?

<?php

/*
Use the following link format:
<a href="goto.php?p=XXXXXX">XXXXXX</a>
*/

$p = $_GET['p'];
$link = array(

/*Links*/
'link1'=>'/link1.htm',
'link2'=>'/link2.htm',

);

/*Send Headers*/

header('Content-Type: text/html; charset=utf-8');
header('X-Robots-Tag: noindex, nofollow, noarchive', true);

if (in_array($p, $link))
{
    header('Location: '.$link[$p]); // Valid p
}  
else
{
    header('Location: /default.htm'); // Invalid p
}

exit();
?>
1
  • Instead of redirecting comment it out and var_dump(in_array($p,$link)) and see if you're getting true or false. If false, you know there's something going on with your $_GET['p'] and/or $link definition. Commented Oct 6, 2012 at 18:56

3 Answers 3

3

use array_key_exists some what like this

if ((array_key_exists('p', $_GET) && isset($link[$p]))
{
    header('Location: '.$link[$p]); // Valid p
}  
else
{
    header('Location: /default.htm'); // Invalid p
}

The above worked for me and also it wont show index undefined warning since you use array_key exists

Sign up to request clarification or add additional context in comments.

1 Comment

I had to change to if (array_key_exists('p', $_GET) && isset($link[$p])) - removed an extra (, but this worked perfectly fine... thank you soo much
1

Instead of:

if (in_array($p, $link))

you want:

if (array_key_exists($p, $link))

The first checks against the values (right of the =>s). The second checks against the keys (left of the =>s).

See: http://php.net/manual/en/function.array-key-exists.php

The short version in your case is:

$link = @$link[$p] ?: '/default.htm';
header("Location: $link");

Which spares you the if and else.

1 Comment

Tried this and the result was a redirect to /$link no matter what value of p is sent.
0

You're checking if the value of $_GET['p'] is in the values-list of $link, but then you're using it as the key in the header() call. It looks like you mean to if $_GET['p'] is a key in the $link array.

To do this, use isset() instead of in_array() to check the array's keys (in_array() will check the array's values):

if (isset($link[$p])) {
    header('Location: '.$link[$p]); // Valid p
}

4 Comments

array_key_exists is probably better then isset here
@Nile Personally, I think array_key_exists() is just a convenience-method for isset(). Also, per benchmark testing, isset() is a lot faster. I haven't confirmed, but I believe that they perform the same low-end tests to check for being set.
according to this, you should use both in case of that weird null case
@Nile The weird null case is what's experienced by inexperienced programmers; the documentation for isset() specifically says it returns false on null - that's why we use it. Also, it's perfectly applicable to the OP's question (so is using array_key_exists(); they both will perform the same task in this case)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.