Load session_start() only if session does not exist?

前端 未结 5 1237
不知归路
不知归路 2020-12-15 22:15

is there a short code trick to check if a session has started and if not then load one? Currently I receive an error \"session already started...\" if I put in a session sta

5条回答
  •  猫巷女王i
    2020-12-15 22:56

    Checking for existing session before starting new:

       // # if phpversion() is >= 5.4
      if(phpversion() >= 5.4) {
    
        // # check session_status() function for value 'PHP_SESSION_NONE'
        if(session_status() === PHP_SESSION_NONE) {
          session_start();
        }
    
      } else { // # maintain backwards compat. with PHP versions older than 5.4
    
          // # if $_SESSION object if NOT set, start a new session!
          if(!isset($_SESSION)) {
            session_start();
          }
      }
    

提交回复
热议问题