我正在为一个有三个不同地点的教堂建立一个网站。主页有一些简要信息,然后列出每个校园。我想存储用户选择的位置,然后在将来访问时将其重定向到该位置。
例如 - 他们访问church.com并在主页上选择location2(church.com/location2)
下次他们在浏览器中输入church.com时,会自动重定向到church.com/location2
谢谢!
答案 0 :(得分:2)
您可以将值存储在cookie ...
中当用户访问时,即位置2:
<?php
$location = 2;
// setcookie(name, value, expirationTime);
setcookie("location", $location, time() + 2592000); // expiration time of one month
?>
在脚本的开头,您必须检查是否已设置“位置”Cookie。如果是 - >重定向到相应的页面
<?php
$location = $_COOKIE["location"];
if(isset($location))
{
header("location: /location".$location);
}
?>
正如我在评论中提到的,我不是WordPress专业人士。但是,以下解决方案适合我。在脚本输出任何内容之前,请不要忘记设置cookie。我将以下行放在模板的header.php
中// get postname (I used postname in 'Permalink Settings')
$location = get_query_var('name');
// if user is on startpage + was not redirected yet -> redirect
// if you don't set the userRedirected cookie, the user is not
// able to visit the startpage anymore to chose a location
if($location == "" && isset($_COOKIE["location"]) && !$_COOKIE["userRedirected"])
{
// path "/" makes the cookie available on the whole domain
setcookie("userRedirected", true, null, "/"); // duration: session
header("location: ".$_COOKIE["location"]); // redirect
}
if($location == "location-1")
{
// set location cookie 1
setcookie("location", "location-1", time() + 2592000, "/");
}
else if($location == "location-2")
{
// set location cookie 2
setcookie("location", "location-2", time() + 2592000, "/");
}
希望有所帮助