Php 有时SetCookie()不工作
嗨,我创建了两个文件来切换我的论坛(中文和英文) enForum.phpPhp 有时SetCookie()不工作,php,setcookie,Php,Setcookie,嗨,我创建了两个文件来切换我的论坛(中文和英文) enForum.php <?php function foo() { global $_COOKIES; setcookie('ForumLangCookie', 'en', time()+3600, '/', '.mysite.com'); echo 'running<br>'; $_COOKIES['ForumLangCookie'] = '
<?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 COOKIESPHP数组名称是$\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');
}