douzhi2017 2016-05-23 08:09
浏览 45

会话变量不可用

I am printing session with print_r($_SESSION) in the index page it shows following array.

 Array
(
    [name] => hhh
)

index.php

  <?php 
  session_start();
  $_SESSION['name']='hhh';

I wanted to unset this variable whenever I want to. So I created a new php file in the same directory with the following contents

    <?php

    session_start();
    echo "before destroying the session";
    print_r($_SESSION);
    unset($_SESSION['name']);//remove the name session variable which is available in my index page.
    session_destroy(); //destroy the session
    echo "after destroying the session";
    print_r($_SESSION);

But whenever I run the above code it prints the following:

before destroying the sessionArray ( ) after destroying the sessionArray ( )

Why my session which is available in the index page is not accessible in the above script's page?.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • duannao3402 2016-05-23 08:19
    关注

    I'm not sure what your goal is, this is what your code does:

    # You start your session
    session_start();
    
    # Echo a string
    echo "before destroying the session";
    
    # Print the $_SESSION array, comes out empty because you havn't put anything in the session
    print_r($_SESSION);
    
    # You unset the 'name' key in the $_SESSION array, which wasn't even there in the first place
    unset($_SESSION['name']);
    
    # You get rid of the session
    session_destroy();
    
    # Echo a string
    echo "after destroying the session";
    
    # You print $_SESSION variable again, which is going to be empty, because you just destroyed the session.
    print_r($_SESSION);
    

    My point explaining your code is, what do you actually wanna do?

    If you want to use a $_SESSION across multiple pages, simply don't session_destroy() between the pages, only destroy it as your user 'logs out' so to speak and session_start() on all your pages.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据