Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 有时SetCookie()不工作_Php_Setcookie - Fatal编程技术网

Php 有时SetCookie()不工作

Php 有时SetCookie()不工作,php,setcookie,Php,Setcookie,嗨,我创建了两个文件来切换我的论坛(中文和英文) enForum.php <?php function foo() { global $_COOKIES; setcookie('ForumLangCookie', 'en', time()+3600, '/', '.mysite.com'); echo 'running<br>'; $_COOKIES['ForumLangCookie'] = '

嗨,我创建了两个文件来切换我的论坛(中文和英文)

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>
<?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>

但有时SetCookie()不起作用。我是否需要添加
睡眠(几秒钟)用于我的代码?

可以使用
$\u COOKIE
访问Cookies,而不是
$\u Cookies


编辑:抱歉误解了。我建议您将变量
$\u COOKIES
更改为另一个常用变量,以便人们能够正确理解您的问题。

可以使用
$\u COOKIE
访问COOKIES,而不是
$\u COOKIES


编辑:抱歉误解了。我建议您将变量
$\u COOKIES
更改为另一个常见变量,以便人们能够正确理解您的问题。

PHP数组名称是$\u COOKIE,而不是$\u COOKIES

PHP数组名称是$\u COOKIE,不是$\u COOKIE

不需要使用
全局$\u COOKIE
-此数组是其中之一,可以从任何地方访问。您可能只包括问题中的一个代码示例,因为它们看起来几乎相同。@mkj,我想用户单击url链接触发不同的语言配置。这就是我创建两个文件的原因。不需要使用
global$\u COOKIE
-此数组是其中之一,可以从任何地方访问。您可能只包括问题中的一个代码示例,因为它们看起来几乎相同。@mkj,我想用户单击url链接触发不同的语言配置。这就是我创建两个文件的原因。@SpanwnCxy,谢谢你的建议。@SpanwnCxy,谢谢你的建议。
$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');
}