PHP 未设置会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37586585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Unset Session Variable
提问by bbowesbo
I'm a noob programmer so I apologies in advance for any obvious mistakes. I've spent the past week creating a product database kinda thing. I've got too the point where I can add products using a form, view all products added etc. I've being using sessions which are created via the form input data. I'm struggling to include get a delete product page working, I've tried using unset to clear the variable but can't get it too work.
我是一个菜鸟程序员,所以我提前为任何明显的错误道歉。过去一周我一直在创建一个产品数据库。我也可以使用表单添加产品,查看添加的所有产品等。我一直在使用通过表单输入数据创建的会话。我正在努力让删除产品页面正常工作,我尝试使用 unset 来清除变量,但无法让它工作。
ADD Product page which sets the session variable:
添加设置会话变量的产品页面:
$_SESSION['Products'][] = $_POST; //is how i set the session on the add products page.
unset $_SESSION['Products'][]; //is how i have tried to clear the session although it does not work.
Any point in the right direction will be appreciated!
任何正确方向的点都将不胜感激!
回答by Thamilan
You can unset session variable using:
您可以使用以下方法取消设置会话变量:
session_unset
- Frees all session variables (It is equal to using:$_SESSION = array();
)unset($_SESSION['Products']);
- Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)session_destroy
— Destroys all data registered to a session
session_unset
-释放所有的会话变量(它等于使用:$_SESSION = array();
)unset($_SESSION['Products']);
- 在会话变量中仅取消设置产品索引。(记住:你必须像函数一样使用,而不是像你使用的那样)session_destroy
— 销毁注册到会话的所有数据
To know the difference between using session_unset
and session_destroy
, read this SO answer. That helps.
要了解使用session_unset
和之间的区别session_destroy
,请阅读此SO 答案。这有帮助。
回答by Don D
Unset is a function. Therefore you have to submit which variable has to be destroyed.
取消设置是一个函数。因此,您必须提交必须销毁哪个变量。
unset($var);
In your case
在你的情况下
unset ($_SESSION["products"]);
If you need to reset whole session variable just call
如果您需要重置整个会话变量,只需调用
session_destroy ();
回答by Jeff Martin
I am including this answer in case someone comes to this page for the same reason I did. I just wasted an embarrassing amount of time trying to track the problem down. I was calling:
如果有人出于与我相同的原因访问此页面,我将包含此答案。我只是浪费了令人尴尬的时间来追踪问题。我打电话给:
unset($_SESSION['myVar']);
from a logout script. Then navigating to a page that required login, and the server still thought I was logged in. The problem was that the logout script was not calling:
从注销脚本。然后导航到需要登录的页面,服务器仍然认为我已登录。问题是注销脚本没有调用:
session_start();
Unsetting a session var DOES NOT WORK unless you start the session first.
除非您先启动会话,否则取消设置会话变量不起作用。
回答by JRsz
If you completely want to clear the session you can use this:
如果你完全想清除会话,你可以使用这个:
session_unset();
session_destroy();
Actually both are not neccessary but it does not hurt.
其实两者都不是必需的,但它并没有伤害。
If you want to clear only a specific part I think you need this:
如果您只想清除特定部分,我认为您需要这样做:
unset($_SESSION['Products']);
//or
$_SESSION['Products'] = "";
depending on what you need.
取决于你需要什么。
回答by mehulmpt
unset
is a function, not an operator. Use it like unset($_SESSION['key']);
to unset that session key. You can, however, use session_destroy();
as well. (Make sure to start the session with session_start();
as well)
unset
是一个函数,而不是一个运算符。使用它unset($_SESSION['key']);
来取消该会话密钥。但是,您也可以使用session_destroy();
。(确保也开始会话session_start();
)
回答by Amin
Destroying a PHP Session
销毁 PHP 会话
A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
一个 PHP 会话可以被 session_destroy() 函数销毁。此函数不需要任何参数,一次调用即可销毁所有会话变量。如果要销毁单个会话变量,则可以使用 unset() 函数来取消设置会话变量。
Here is the example to unset a single variable
这是取消设置单个变量的示例
<?php unset($_SESSION['counter']); ?>
Here is the call which will destroy all the session variables
这是将销毁所有会话变量的调用
<?php session_destroy(); ?>