php设置一个cookie并通过自动实现更改其值


php set a cookie and change its value with auto actualisation

目前正在创建一个小型网站,我正在尝试添加一个介于法语和英语之间的多语言工具。但我面临着一些问题。基本上,我有两个按钮,英语和法语来更改语言。但如果是第一次访问,则应设置默认语言(此处为法语)。

所以我创建了一个小脚本,但并没有像我预期的那样工作。。。

if(!isset($_COOKIE['lang_ylx'])) {
 $timestamp_expiration = time()+30*24*3600 ;
if(!isset($_GET['lang'])) {                      
    setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true);
}
else if ($_GET['lang']=='fr') {      // si la langue est 'fr' par le bouton de langue, on définit le cookie francais
    setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true); //On définit un cookie de langue
    header('Location: index.php');  
} 
else if ($_GET['lang']=='en') { 
    setcookie('lang_ylx', 'en',$timestamp_expiration, null, null, false, true); 
    header('Location: index.php');  
}

该脚本会自动加载到带有include的index.php中。目前,我只有index.php,当我点击例如英文按钮时,我正在尝试在URL中传递一个参数,然后更改cookie,使其获得"en"值,然后返回index.php。

我的lang文件是两个巨大的循环,用来测试cookie的值。但每当我点击英语或法语按钮时,我都会被困在lang.php上,但它应该回到index.php,使用新的langage。

有什么想法吗?

$timestamp_expiration = time()+30*24*3600 ;
if(!isset($_COOKIE['lang_ylx'])) {
    //If you want your default to be english, use this:
    setcookie('lang_ylx', 'en', $timestamp_expiration, null, null, false, true);
    //French:
    setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true);
}
/*if(!isset($_GET['lang'])) {                      
    setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true);
}*/
//Comment out above because you're setting french every time llang isn't set.
if (isset($_GET['lang']){
    else if ($_GET['lang']=='fr') {      // si la langue est 'fr' par le bouton de langue, on définit le cookie francais
        setcookie("lang_ylx", "", time()-3600);
        setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true); //On définit un cookie de langue
    header('Location: index.php');  
    } 
    else if ($_GET['lang']=='en') {
        setcookie("lang_ylx", "", time()-3600);
        setcookie('lang_ylx', 'en',$timestamp_expiration, null, null, false, true); 
        header('Location: index.php');  
    }
}

你有:

  1. 安在错误的地方
  2. 不设置任何默认值