I’m helping put together a site that’s going to host content for different regions on the same domain. So example:
http://www.example.com/us/
http://www.example.com/uk/
http://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.: http://www.example.com/us/contact.php) and their preference cookie says /fr/, how do I best forward them to http://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?
so you can add a check to preg_match. It prevents to execute rest of the code.