1

您好我创建了两个文件来切换我的论坛(语言中文和英文)

enForum.php

<?php

     function foo() { 
        global $_COOKIES; 
        setcookie('ForumLangCookie', 'en', time()+3600, '/', '.mysite.com'); 
        echo 'running<br>'; 
        $_COOKIES['ForumLangCookie'] = 'en'; 
        bar(); 
    } // foo() 


    function bar() { 
      global $_COOKIES; 
      if (empty($_COOKIES['ForumLangCookie'])) { 
              die('cookie_name is empty'); 
            } 
            echo 'Language  =' . $_COOKIES['ForumLangCookie'];
            echo "<br>";
    } // bar() 

    foo();


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forum EN Version</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

please be patient ...
<script LANGUAGE='javascript'>

   location.href='http://www.mysite.com/forum/index.php';

</script>


</body>
</html>

cn论坛.php

<?php

     function foo() { 
        global $_COOKIES; 
        setcookie('ForumLangCookie', 'cn', time()+3600, '/', '.mysite.com'); 
        echo 'running<br>'; 
        $_COOKIES['ForumLangCookie'] = 'cn'; 
        bar(); 
    } // foo() 


    function bar() { 
      global $_COOKIES; 
      if (empty($_COOKIES['ForumLangCookie'])) { 
              die('cookie_name is empty'); 
            } 
            echo 'Language  =' . $_COOKIES['ForumLangCookie'];
            echo "<br>";
    } // bar() 

    foo();


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forum CN Version</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

please be patient ...
<script LANGUAGE='javascript'>

   location.href='http://www.mysite.com/forum/index.php';

</script>
</body>
</html>

有一些文件,包括include template('logon');include template('regist');,我编写了一些代码来获取 Cookie 值并控制流程以加载不同的模板文件。

$lang = $_COOKIE["ForumLangCookie"];
// for Debug
// echo '$lang is '.$lang;
// echo '<br/>';

if ($lang == "cn"){
    include template('logon');
}
else if ($lang == "en"){
    include en_template('logon');
}

但有时 SetCookie() 不起作用。我需要添加Sleep(someSeconds);我的代码吗?

4

2 回答 2

1

可以使用$_COOKIE,not访问 Cookie $_COOKIES

编辑:对不起,误解。我建议您将变量更改$_COOKIES为另一个常见变量,以便人们可以正确理解您的问题。

于 2010-04-18T09:26:50.167 回答
0

PHP 数组名称是 $_COOKIE,而不是 $_COOKIES

于 2010-04-18T09:28:36.167 回答