PHP多语言网站与cookie

时间:2018-03-10 20:59:26

标签: php html cookies

我想用基于COOKIE的PHP创建一个多语言网站。

我尝试使用此代码,但每次刷新页面时,语言都会更改:

的header.php

<div class="header-language">
<?php
    foreach ($lingue as $k=>$v)
    {
         if ($k != $lingua)
         {
             setcookie("lang", $k, time() + (86400 * 7), '/');
             ?>
             <a href=""><img src="bandiere/<?php echo $flags[$k]; ?>.png" alt="<?php echo $v; ?>" title="<?php echo $v; ?>" border="0" /></a>
             <?php
         }
    }
?>
</div>

gestore.php

<?php 
$lingue = array(1=>'Italiano', 'English'); 
$choose = array(1=>'Scegli la lingua', 'Choose language'); 
$flags  = array(1=>'it', 'uk');

$lingua = $_COOKIE['lang'];

switch ($lingua)
{
    case 1:
        include "lingue/it.php";
        break;
    case 2:
        include "lingue/uk.php";
        break;
}
?>

cookie.language.php

<?php 
$cookie_name = "lang"; 
$cookie_value = 1; 
//Check if there is already the cookie
if(!isset($_COOKIE['lang']))
{
    //Create the language cookie
    setcookie($cookie_name, $cookie_value, time() + (86400 * 7), "/"); // 86400 = 1 day
}

任何人都可以帮助我。感谢

0 个答案:

没有答案