Store variable using sessions

前端 未结 1 557
渐次进展
渐次进展 2021-01-23 02:40

I have a variable which is updated on every page shift, but I want to store the value in the first call for good somehow.

The variable is e.g

   $sizeOf         


        
相关标签:
1条回答
  • 2021-01-23 03:22

    session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

    When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. These will either be a built-in save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be custom handler as defined by session_set_save_handler(). The read callback will retrieve any existing session data (stored in a special serialized format) and will be unserialized and used to automatically populate the $_SESSION superglobal when the read callback returns the saved session data back to PHP session handling.

    So, on every page make sure to start it with:

    <?php
    session_start();
    

    Then, you set the value like this:

    if(!isset($_SESSION['name'])) {
        $_SESSION['name'] = $sizeOfSearch;
    }
    

    Whenever you need the retrieve the value use this:

    print $_SESSION['name'];
    

    This session will keep store the variable as long as you don't destroy it. Code for destroying a session:

    session_destroy();
    
    0 讨论(0)
提交回复
热议问题