[英]What's wrong with this PHP $_COOKIE code snippet?
I have an input='text' name='fileName'
element and want to store its value in the cookie filename
. 我有一个
input='text' name='fileName'
元素,想将其值存储在cookie filename
。 When first created, cookie filename
is assigned default constant DEFAULT_FILE_NAME
If the request variable fileName
is set (thru POST method), then cookie filename
is assigned the value of this request variable. 首次创建时,为cookie
filename
分配了默认常量DEFAULT_FILE_NAME
如果设置了请求变量fileName
(通过POST方法),则为cookie filename
分配了此请求变量的值。 Otherwise, the cookie retains its old value. 否则,cookie保留其旧值。 .
。 I try to do it using the following snippet:
我尝试使用以下代码段进行操作:
if (!isset($_COOKIE['filename'])) {
setcookie('filename', DEFAULT_FILE_NAME);
$_COOKIE['filename'] = DEFAULT_FILE_NAME;
}
if (isset($_REQUEST['fileName'])) {
setcookie('filename', $_REQUEST['fileName']);
$_COOKIE['filename'] = $_REQUEST['fileName'];
}
The problem is that when the request variable fileName
is not set, the cookie is recognized as non-existent, and is assigned DEFAULT_FILE_NAME again. 问题在于,当未设置请求变量
fileName
,该cookie被识别为不存在,并再次被分配了DEFAULT_FILE_NAME。 What goes wrong? 怎么了?
I have one form and three submit buttons on it which serve as main menu. 我有一个表单,上面有三个提交按钮,它们可以用作主菜单。 If Submit #1 is clicked then I draw the UI for specifying file name ('fileName' element).
如果单击“提交#1”,那么我将绘制用于指定文件名(“ fileName”元素)的UI。 User enters file name and clicks OK and after that it is assigned to the cookie 'filename' because file name is used throughout the app.
用户输入文件名并单击“确定”,然后将其分配给Cookie“文件名”,因为在整个应用程序中都使用了文件名。 (Of course, when user clicks Submit #1 and the cookie 'filename' is already set, I write its value to the 'fileName' element.).
(当然,当用户单击Submit#1且cookie'filename'已设置时,我将其值写入'fileName'元素。)。 While I am within the UI created by the Submit #1 and 'fileName' elem is visible, everything is OK and the cookie is set/read successfully.
当我处于由Submit#1创建的UI内并且可见“ fileName”元素时,一切正常,并且cookie被成功设置/读取。 But when I click submit #2 which draws UI for another needs, but uses the cookie 'filename', that's when the problem arises.
但是,当我单击提交#2时,它会绘制UI来满足其他需求,但使用Cookie的“文件名”,这就是问题所在。 I find that the cookie 'filename' loses its value read from the 'fileName' element and is assigned default value again.
我发现cookie'filename'失去了从'fileName'元素读取的值,并再次被赋予默认值。
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
一旦设置了cookie,就可以使用$ _COOKIE或$ HTTP_COOKIE_VARS数组在下一页加载时访问它们 。 Note, superglobals such as $_COOKIE became available in PHP 4.1.0.
注意,诸如$ _COOKIE之类的超全局变量在PHP 4.1.0中可用。 Cookie values also exist in $_REQUEST.
Cookie值也存在于$ _REQUEST中。
Try this: 尝试这个:
if (!isset($_COOKIE['filename'])) {setcookie('filename')}
if (isset($_REQUEST['fileName'])) {
setcookie('filename', $_REQUEST['fileName']);
}
else{
setcookie('filename', DEFAULT_FILE_NAME);
}
UPDATE: Maybe this: 更新:也许是这样的:
if (!isset($_COOKIE['filename'])) {setcookie('filename'),DEFAULT_FILE_NAME,time()+3600);}
if (isset($_REQUEST['fileName'])) {setcookie('filename', $_REQUEST['fileName'],time()+3600);}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.