1

I get an error saying 'Undefined index: redirect'

with this code:

        $came_from_site = $_GET['redirect'];
        if($came_from_site != "true")
        {
            echo 'USER TYPED IN URL MANUALLY';
        }

The error happens when the query string doesn't exist.... So I guess I need to check if a) it exists and then if it does b) check that it's value is true

Can anyone help?

Thanks

3
  • 2
    $came_from_site = isset($_GET['redirect']) ? $_GET['redirect'] : null; Commented Aug 31, 2011 at 9:19
  • Hi could you explain what each bit of that line of code does as I don't understand it. Thanks ;) note: it works great by the way Commented Aug 31, 2011 at 9:23
  • The above code uses ternary operators and is a shorthand for an if/else statement, albeit confusing sometimes, they are handy to save time and space. Read the above as: assign $came_from_site the return value of $_GET['redirect'] if isset($_GET['redirect']) evaluates to true. Else, set $came_from_site to null. Commented Aug 31, 2011 at 9:28

1 Answer 1

1
if(empty($_GET['redirect'])){
    echo 'USER TYPED IN URL MANUALLY';
}

Thanks

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

1 Comment

use empty() or !empty() as isset() will not check the value but only existence of variable whereas empty will check the existence of variable and wether it is true,false or 0

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.