PHP: the best way to redirect a user to another URL dynamically

I help to build a website that will host content for different regions in the same domain. So an example:

www.example.com/us/
www.example.com/uk/
www.example.com/fr/
et al.

I have a system that asks the user which site they want to view (and then saves their preferences in a cookie). My question is: if they visit the URL (for example: www.example.com/us/contact.php) and their cookie preference says / fr /, how should I send them to www.example.com/fr/contact . Php?

The system can read what area of ​​the site they are in and what their cookie says. So the information we would know would be: Site: USA and Cookie: FR.

I thought to use $_SERVER['SCRIPT_NAME']and use regex to get "contact.php" from the url. Then, using header("Location: [url]");, but I understand that it Locationdoesn’t work if any text is already transferred to the browser ... which creates all kinds of problems.

Edit:
Here are a few examples to more clearly explain the problem:

<?php
// Get variable contents for $cookieRegion and $siteRegion
if($cookieRegion != '') { // If Cookie has been previous set
    if($cookieRegion != $siteRegion) { // If Cookie pref clashes with site URL
       // Forward to correct URL
    }
}
else { ?>
    <script type="text/javascript">
        $(document).ready(function(){
            // Display modal window asking user preference
        });
    </script>        
<?php } ?>

So, the tag <script>will be placed before the start of the document ... not a good idea!

What is the best way to get around this?

Or is there a better way to handle the whole problem that I could implement instead?

+3
source share
4 answers
<?php
// Use this to open modal window.
$wrongRegion = FALSE;

// if there is a cookie...
if (isset($_COOKIE['region']))
{
    // I am using preg_match to ensure we are getting right parameters...
    // Sorry I am not good with Regex. You can set a better patern.
    preg_match("#/(.*?)/(.*?)\.php#is", $_SERVER['REQUEST_URI'], $m);

    // Okay! $m[1][0] is the region code on the uri. lets check if its same with cookie
    if ($_COOKIE['region'] != $m[1][0])
        $wrongRegion = TRUE;
}

....... (codes goes here)

// to where you put the modal window code:

if ($wrongRegion == TRUE)
{
    // put the modal window code.
}

so you can add a check to preg_match. This prevents the rest of the code from executing.

+2
source

- HTML .

<?php
# This will split the request URI into an array with all the components between slashes in the URI
$request_parameters = explode('/', $_SERVER['SCRIPT_URI']);

# Now, hopefully $request_parameters[1] contains the language abbreviation/code
if ( $request_parameters[1] != $cookieLanguage ) {
    # Replace the erroneous language with the language saved in the cookie, 
    # reassemble the URI and send the client to the correct location
    $request_parameters[1] = $cookieLanguage;
    header('Location: ' . implode('/', $request_parameters);
}

cookie

  • , . /en-US/contact/
    • ; cookie .
+1

cookie, , (contact.php), Location: , index.php

0

.

script ( html.. !) - :

 <?

 if $locationCookie == 'fr') {

 header("Location: /fr");

 //or alternatively
 //header("Location: /fr/contact.php");

 }

 //also, let say they are on the us/contact.php page and you want them to go to the fr/contact.php page

 header('Location: ../'.$language_cookie.'/contact.php');

 ?>

, , , $_GET, fr. .

-1

All Articles