我已经制作了一个以三种不同语言工作的PHP网站。这些语言可以通过$ _GET或者$ _COOKIE来设置,$ _GET是第一个被检查的,所以如果需要的话它可以覆盖cookie。如果我确定了我需要的语言,我会包含一个包含我所有翻译的文件并设置一个cookie供将来使用。该代码看起来是这样的:Cookie未被正确保存/正确读取
<?php
if (isset($_GET['language'])) {
// if language in $_SET is English, load english translation and set a cookie for the future.
if ($_GET['language'] == 'EN') {
setcookie('language','EN', time()+31536000);
include 'tekstenEngels.php';
// if language in $_SET is French, load french translation and set a cookie for the future.
} elseif ($_GET['language'] == 'FR') {
setcookie('language','FR', time()+31536000);
include 'tekstenFrans.php';
// lastly if language in $_SET is Dutch, load Dutch translation and set a cookie for the future
} else {
setcookie('language','NL', time()+31536000);
include 'tekstenNederlands.php';
}
// the same but for cookies in case this isn't the first visit
} elseif (isset($_COOKIE['language'])) {
if ($_COOKIE['language'] == 'EN') {
include 'tekstenEngels.php';
} elseif ($_COOKIE['language'] == 'FR') {
include 'tekstenFrans.php';
} else {
include 'tekstenNederlands.php';
}
}
?>
这个工作对我的locahost不错,但它似乎并没有写/我的web服务器一起使用时,正确读取的cookie。任何想法我可能会错过/做错了什么?
Cookies已启用。有时我的代码就像一个魅力,它会记住所有正确的事情,而其他尝试我需要刷新我的页面以便代码成功执行。这真的让我发疯,因为我似乎无法在这里找到问题。 – Rijstkoek
要追踪它,尝试将代码剥离到最小。例如:只有发布的代码后面跟着print_r($ _ COOKIE),没有任何其他条件,如果和其他条件。 如果这仍然是不可靠/随机的,它可能是一个服务器性能问题。有时[输出缓冲](http://php.net/manual/it/function.ob-start.php)可以解决这些幽灵般的错误。 – Joram