cookie 是正确的方法。
你不能实现跨浏览器的东西......你可以使用 Flash cookie 或重新检测 IP 地址等,但那很愚蠢......
craiglist.org 似乎会根据我的 ip 地址来自哪里来重定向我。
要做到这一点,你应该看看这个网站:http://www.maxmind.com/app/geolitecity
一般来说,您应该始终为用户提供手动切换的可能性。
从可用性优化的角度来看,我建议以下方案:
- 用户点击欢迎页面
- 欢迎页面使用 geoip 检测用户所在城市
3.1 如果城市被成功检测到用户被直接跳到子站点,cookie被保存
3.2 如果没有成功检测到城市,欢迎页面显示一个选择列表。一旦用户点击一个选择列表,cookie就会被设置并且用户得到指定的子站点
- 在每个子站点的顶部“切换城市”或其他东西上都有一个清晰可见的按钮。
如果用户点击它,他会进入欢迎页面,但是带有参数“userelection=true” ro 的东西,因此自动 ip 检测和重定向不会启动。
用户选择所需的子站点,获取 cookie 并被重定向。
5.
如果用户再次点击欢迎页面并找到 cookie,他会被重定向到子站点,cookie 可以被覆盖,没问题...
最简单的解决方案是使用 php 设置 cookie,然后进行标头重定向!
这样你就消除了javascript,可以关闭什么的。
只需使用这个 php 命令:
setcookie("city", $value, time()+(86400*365));
或类似的东西。这是手册页:
http://php.net/manual/en/function.setcookie.php
您可以使用
检查 cookie 值
if($_COOKIE['city'] != '')
编辑:
除了困惑我现在在这里为您提供了一个工作示例,只需将其粘贴到 php 文件中并将其保存到您的网络服务器:) 不需要检查子站点,因为您说您正在重定向到外部网站(子域)反正。
请记住,如果您将其设置在另一个子域上,则无法从子域访问 cookie!可能这是你的问题,因此检查和重定向到他的引导文件。
好吧,你去吧。任何问题?评论!
<?
/* this is if we are at a subsite. you wont need that as it will be external subdomains anway just for the purpose of a working example here.*/
if($_GET['subsite']!="") {
/* the change link has the paramenter userselection which overrides any autoamtic detection by cookie or ip or whatever, this is important if the user wants to change or the automatic change fails or is wrong*/
?>
you are now at the subsite of <?=$_GET['subsite'];?><br/>
<a href="?userselection=true">click here to change</a>
<?
}
else {
/* only try to automatically redirect if we have either cookie or user specified data, but dont if the flag userselectino is true. because the user wants to change.
you could ip-detect the city at this point.
*/
if(($_GET['go']!=''||$_COOKIE['city']!='')&&$_GET['userselection']!='true') {
/* if we had ip detection the priority of it would probably go here */
$target=$_COOKIE['city'];
/* the get varaible overrides the cookie value as its user supplied and more important*/
if($_GET['go']!='') {
$target=$_GET['go'];
}
/* set the cookie, don't care if it already has been set*/
setcookie('city',$target,time()+(86400*365));
/* redirect the user */
header('Location: ?subsite='.$target);
}
else {
/* well we either have neither cookie nor user specified city or the user wanted to change teh city, lets display the selection dialog!*/
?>
hello! select your city:<br/>
<a href="?go=vienna">vienna</a>
<a href="?go=newyork">new york</a>
<?
}
}
?>