views:

49

answers:

1

Hi. I have a little problem with redirecting. Registered users follows this link site.com/reg.php?passkey=1234 but the first the user get redirected to the correct language based on a cookie. I need to keep the passkey variable when the user is redirected. like this ?lang=en_US&passkey=1234

My code so far look something like this:

if (!isset($_GET['lang']))
{

        if (isset($_COOKIE['country'])) 
        {

                $country = $_COOKIE['country'];

                (...)

                elseif  ( $country == "US" ){       

                $variables = $_GET;
                $variables['lang'] = "en_US";

                header('Location: ?' . http_build_query($variables));

                exit();
                }   

This works:

reg.php
reg.php?lang=en_US
reg.php?lang=en_US&passkey=test
reg.php?passkey=test&lang=en_US

but this gives an The page isn't redirecting properly error

reg.php?passkey=test

I don't understand why this doesn't work when all the other combinations seem to work perfectly.

A: 

The HTTP 1.1 Specification requires that the location needs to be a Absolute URI (See RFC2616 14.30 Location)

The location header('Location: ?' . http_build_query($variables)); does not contain an absolute URI.

You need something like:

header('Location: /folder/file.php?'.http_build_query($variables));

If you need to do this on different locantions you can use $_SERVER['PHP_SELF'] to set the current file as redirect location. For example

header('Location: '.$_SERVER['PHP_SELF'].'?'.http_build_query($variables));
Dubas
That won't work for my site and it seem to work without the full location. I think the problem is that it keeps redirecting endless amount of times for some reason.
ganjan
You can get the path of the current File into your server if you require to reutilize this code into diferent files from diferent sources for example with $_SERVER['PHP_SELF']
Dubas
header('Location: '.$_SERVER['PHP_SELF'].'?' . http_build_query($variables)); gives the same result
ganjan
If this don't solve the problem probably you are ommiting a part of code that is important to solve the problem.Whats the part of code when $_GET['lang'] is not set and $_COOKIE['country'] is not set?
Dubas