我正在帮助组建一个网站,该网站将为同一个域中的不同区域托管内容。 例如:
www.example.com/us/ www.example.com/uk/ www.example.com/fr/ 等等
我有一个系统,询问用户他们想要查看哪个网站(然后将他们的偏好存储在cookie中)。 我的问题是:如果他们访问了一个网址(例如:www.example.com/us/contact.php)并且他们的偏好Cookie显示/ fr /,我该如何最好地将它们转发到www.example.com/fr/contact。 PHP的?
系统可以读取他们所在的站点区域以及他们的cookie所说的内容。 所以我们知道的信息将是:网站:美国和Cookie:FR。
我正在考虑使用$_SERVER['SCRIPT_NAME']并使用正则表达式从URL获取“contact.php”。 然后使用header("Location: [url]"); ,但据我所知,如果任何文本已经传递给浏览器,则Location不起作用...这会产生各种问题。
编辑: 这里有一些代码可以更清楚地解释问题:
<?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 } ?>所以<script>标签将放在文档开始之前......不是一个好主意!
解决这个问题的最佳方法是什么?
或者,有没有更好的方法来处理我可以实现的整个问题?
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. }所以你可以给preg_match添加一个检查。 它阻止执行其余代码。
<?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.
更多推荐
发布评论