1

I am trying to write a php script for following...

if country = in or ca
redirect_to http://example.com
else redirect to http://example2.com

if $_GET['foo'] = 'bar' and country = in or ca
redirect to http://example3.com

else redirect to example.com
else redirect to example.com

and I have tried following...

$country = $_SESSION['countrycode'];

//if country india,canada
if($country == 'in' || $country == 'ca') redirect_to('http://example.com/');
else redirect_to('http://example2.com/');

//if querysting contains foo=bar and country india,canada
if($_GET['foo'] == 'bar'){
    if($country == 'in' || $country == 'ca') redirect_to('http://example3.com/');
    else redirect_to('http://example.com/');
}
else redirect_to('http://example.com/');

But its not working in case of query string foo = bar. Please suggest.

5
  • 1
    Well, what is the value of $country? Commented Nov 19, 2013 at 1:22
  • @relentless iso code from session like 'in' or 'ca'; Commented Nov 19, 2013 at 1:23
  • Are you sure it's set? echo $country and check the actual value. Also, is $_GET['foo'] set? Commented Nov 19, 2013 at 1:24
  • I guess I don't understand what you're asking... Commented Nov 19, 2013 at 1:28
  • @relentless everything is set. Commented Nov 19, 2013 at 1:32

1 Answer 1

2

Your first conditional statement is redirecting the page away from this script. The second conditional statement is never read. Instead, you should refactor something like this:

if($country == 'in' || $country == 'ca'){
    if($_GET['foo'] === 'bar')
        header('Location: ' . $urlof3);
    else
        header('Location: '. $urlof2);
}

Cheers

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

1 Comment

Yes. You need to start with your most specific redirect conditions and follow with most general, otherwise more specific cases will never be reached.

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.