What exactly is session_id( ) and session_name( )? Explain how they are being used in the following code
PHP uses cookies to manage sessions; specifically, by setting an identifying key/value pair for that session inside a cookie.
- The name of the session is the name of the cookie; the default name for PHP-based websites is
PHPSESSID
.session_name()
returns the session name or, if a parameter is passed, updates the session name. - The key/value pair inside the cookie describes the session id; the key denotes that it is the session identifier, and the value is the session identifier itself.
session_id()
returns the session id or, if a parameter is passed, updates the session id.
The code in the question checks if there is session passed with the request: first by starting/reactivating the session with session_start()
, then checking for an existing cookie matching the session name. If the code finds one, it forces the browser to remove the cookie by setting its expiration date to a time in the past.