duanfengwang9157 2017-02-16 19:37
浏览 37

如何从php中的$ _SESSION数组中完全删除空键?

I start the session by following command:

session_start();

when the user logs in I save information of that user in the $_SESSION with following code:

$_SESSION["logged_in_user"]=$username;  
$_SESSION["logged_in_status"]="logged in";
$_SESSION["logged_in_business_id"]=$business_id;
$_SESSION["logged_in_business"]=$business_name;

and when the user logs out I do the following commands to clear the $_SESSION :

setcookie("PHPSESSID", "", time()-3600, "/" );
$_SESSION=array();
session_destroy();
session_write_close();

Now when I check the $_SESSION after logout and before login, the result is an empty array(), which is exactly what it should be. But as soon as I log in $_SESSION not only shows the information I saved in it, but also empty values with filled keys, like for example:

Array(
[logged_in_user] => aristocraticmasterminder
[logged_in_status] => logged in
[logged_in_business_id] => us-000000001
[logged_in_business] => Microsoft PLC Limited
[RoleID] => 
[RoleName] => 
[RoleDuty1] => 
);

These keys do not have any value saved in $_SESSION corresponding to them. I session_start(); on the top of each page, and consequently also on the log out page before session_destroy(); I think this has something to do with the PHPSESSID cookie which is saved on my browser. How can I clear these empty keys and just save the data saved after the session_start();?

  • 写回答

2条回答 默认 最新

  • douyannuo7733 2017-02-16 19:45
    关注

    try this

        unset($_SESSION['name']);
    

    How to remove a variable from a PHP session array

    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿