ClaudiaWhite 11 Questions 0 Answers 0 Best Answers 20 Points View Profile 0 ClaudiaWhite Asked: April 17, 2020In: PHP How does a PHP session work internally? 0 How does a PHP session work internally? php Share Facebook 1 Answer Voted Oldest Recent Mohd Tariq United Arab Emirates 0 Questions 10 Answers 0 Best Answers 40 Points View Profile tariq Added an answer on April 28, 2020 at 6:02 am A PHP session follow a simple workflow. Whenever a session is started PHP looks for an existing session by checking PHPSESSID value usually stored in browser cookie or passed in URL depending on the settings in php.ini. If PHPSESSID value is not set then PHP will generate a new session_id and a new file will be created in a temporary directory (or whatever path is specified in php.ini) on the server with the name sess_ session_id (session_id value prefixed with sess_). The newly generated session_id value is then set in the cookie in PHPSESSID key or will be passed in the URL in subsequent calls. If PHPSESSID value is set then PHP searches for the session file in the specified path that matches PHPSESSID prefixed with sess_, retrieves it’s content, un-serialize the content and assign the result to $_SESSION global array. When we add or set data in session the data gets stored in $_SESSION global array and then it gets saved in the corresponding session file after serializing $_SESSION data. The session gets destroyed after the predetermined period of session time expires (as per php.ini settings) or session_destroy function is called. A PHP session also gets destroyed when the user closes the browser as by default the PHPSESSID cookie lifetime is set to 0 (which means until browser is closed). 0 Reply Share Share Share on Facebook Share on Twitter Share on LinkedIn Share on WhatsApp Sorry, you do not have a permission to answer to this question.