How to print all session variables currently set?

后端 未结 7 2040
眼角桃花
眼角桃花 2020-12-12 18:56

Without having to call each session variable by name, is there a way to display the content of all the session variables currently set?

相关标签:
7条回答
  • 2020-12-12 19:18
    session_start();
    echo '<pre>';var_dump($_SESSION);echo '</pre>';
    // or
    echo '<pre>';print_r($_SESSION);echo '</pre>';
    

    NOTE: session_start(); line is must then only you will able to print the value $_SESSION

    0 讨论(0)
  • 2020-12-12 19:20
    <?php
        session_start();
        echo "<h3> PHP List All Session Variables</h3>";
        foreach ($_SESSION as $key=>$val)
        echo $key." ".$val."<br/>";
    ?>
    
    0 讨论(0)
  • 2020-12-12 19:24

    Not a simple way, no.

    Let's say that by "active" you mean "hasn't passed the maximum lifetime" and hasn't been explicitly destroyed and that you're using the default session handler.

    • First, the maximum lifetime is defined as a php.ini config and is defined in terms of the last activity on the session. So the "expiry" mechanism would have to read the content of the sessions to determine the application-defined expiry.
    • Second, you'd have to manually read the sessions directory and read the files, whose format I don't even know they're in.

    If you really need this, you must implement some sort of custom session handler. See session_set_save_handler.

    Take also in consideration that you'll have no feedback if the user just closes the browser or moves away from your site without explciitly logging out. Depending on much inactivity you consider the threshold to deem a session "inactive", the number of false positives you'll get may be very high.

    0 讨论(0)
  • 2020-12-12 19:24

    this worked for me:-

    <?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>'; ?>

    thanks for sharing code...

    Array
    (    
        [__ci_last_regenerate] => 1490879962
    
        [user_id] => 3
    
        [designation_name] => Admin
        [region_name] => admin
        [territory_name] => admin
        [designation_id] => 2
        [region_id] => 1
        [territory_id] => 1
        [employee_user_id] => mosin11
    )
    
    0 讨论(0)
  • 2020-12-12 19:26

    You could use the following code.

    print_r($_SESSION);
    
    0 讨论(0)
  • 2020-12-12 19:26

    Echo the session object as json. I like json because I have a browser extension that nicely formats json.

    session_start();
    echo json_encode($_SESSION);
    
    0 讨论(0)
提交回复
热议问题