我在index.php wordpress网站上有这个重定向 它将使用保加利亚IP的用户重定向到我的本地站点 可以为已经重定向一次的用户关闭此脚本 我希望用户已被重定向,并希望阅读英文版本 能够阅读它而不是再次重定向。
<?php
$server = 'test'; // MySQL hostname
$username = 'test'; // MySQL username
$password = 'test'; // MySQL password
$dbname = 'test'; // MySQL db name
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$sql = 'SELECT
country
FROM
ip2nation
WHERE
ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
ORDER BY
ip DESC
LIMIT 0,1';
list($country) = mysql_fetch_row(mysql_query($sql));
switch ($country) {
case 'bg':
// Get the bg to a bg site
header('Location: http://bg.test.com');
exit;
}
我怎样才能使用cookie或其他内容。
答案 0 :(得分:0)
这样的事情应该有效:
<?php
[.....]
list($country) = mysql_fetch_row(mysql_query($sql));
/* ****** NEW CODE ******* */
if ( isset( $_COOKIE['bg_redirect'] ) && $_COOKIE['bg_redirect'] == 'true' ){
// cookie exists! user has already been sent once
$country = ''; // reset so it's NOT 'bg'
}
switch ($country) {
case 'bg':
/* ****** NEW CODE ******* */
// set cookie
setcookie( 'bg_redirect', 'true', time()+ 86400 ); // set cookie for 24 hours
// Get the bg to a bg site
header('Location: http://bg.test.com');
exit;
}
您还可以显示已重定向的访问者的链接,以便重置为:
<?php if ( isset( $_COOKIE['bg_redirect'] ) ) { ?>
<a href="/?reset=true">Back to BG</a>
<?php } ?>
然后在代码的最顶层:
<?php
if ( isset( $_REQUEST['reset'] ) ){
// expire cookie
unset($_COOKIE['bg_redirect']);
setcookie( 'bg_redirect', null, -1 );
}