开发者

PHP: Best way to forward a user to a different URL dynamically

开发者 https://www.devze.com 2023-03-08 23:34 出处:网络
I\'m helping put together a site that\'s going to host content for different regions on the same domain. So example:

I'm helping put together a site that's going to host content for different regions on the same domain. So example:

www.example.com/us/

www.example.com/uk/

www.example.com/fr/

etc.

I have a system in place that asks the user which site they want to view (and then stores their preference in a cookie). My question is: If they visit a URL (e.g.: www.example.com/us/contact.php) and their preference cookie says /fr/, how do I best forward them to www.example.com/fr/contact.php?

The system can read what site region they're on and what their cookie says. So the information we would know would be: Site: US and Cookie: FR.

I was thinking of using $_SERVER['SCRIPT_NAME'] and using regex to get "contact.php" from the URL. Then using header("Location: [url]");, but I understand that Location doesn't work if any text has already been passed to the browser... which creates all sorts of problems.

Edit:

Here's some code to explain the problem more clearly:

<?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 <script> tag would be placed before the document start... not a good idea!

What's the best way to get around this problem?

Or, is there a better way of handling the whole problem that I could implement instead?


<?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. It prevents to execute rest of the code.


Put the following code, or some equivalent code before ANY HTML or linebreaks.

<?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);
}

If there is not any cookie set

  1. Show the default page e.g. /en-US/contact/
  2. Let the client choose language
    • If and when the client chooses a language; set the language cookie and reload the page.


Read the cookie first, then parse where the user wants to go (contact.php), then redirect it with Location: before sending output, so at the beginning of index.php


This is actually very simple.

At the beginning of your script (before any html.. especially the tag!) do something along these lines:

 <?

 if $locationCookie == 'fr') {

 header("Location: /fr");

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

 }

 //also, let's 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');

 ?>

What this will do is redirect the user to the SAME PAGE they are currently on, but will have the $_GET variable of language equal to fr. Then, in your page you should check for this variable and make the proper changes accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消