首页 > 解决方案 > php标头重定向忽略条件语句

问题描述

我正在使用带有 Geo Target 插件的 Wordpress,预期行为是,如果网站访问者在德国,他们将被重定向到网站的德语版本,并且可以选择单击链接以查看英文版本。我这样做是通过设置会话 cookie 来忽略主页上的重定向(如果它们已经发送到德语版本)。我遇到的问题是它忽略了 $redirectedBefore 的部分 if 语句,并且总是重定向到 /de/。这是我的代码:

<?php
require( dirname( FILE ) . '/wp-load.php' );
$country = do_shortcode( '[geoip-country]' );
//echo "Country: " . $country . "<br />";
//This does work and outputs the correct country code at the top of the homepage

if (isset($_COOKIE) && isset($_COOKIE['langPref'])){
$redirectedBefore = true;
} else {
$redirectedBefore = false;
}

if ( $country == ( 'DE' ) && !$redirectedBefore ) {
//header("Set-Cookie: langPref=1; expires=0; path=/;");
//Trying a couple ways to set cookie
setcookie("langPref", "1", 0, "/");
header("Location: /de/");
exit;
} else {
//header("Set-Cookie: langPref=1; expires=0; path=/;");
//Trying a couple ways to set cookie
setcookie("langPref", "1", 0, "/");
}
?>

先感谢您

标签: phpwordpressredirectheadergeotargetting

解决方案


推荐阅读