How to Create a Session in PHP
You may have heard the phrase “Your session has expired” or encountered it yourself when trying to access some website data that was previously accessed (like login credentials for example). This is because the browser’s cookie, which contains the user’s session id, was deleted and as a result their session has ended.
Sessions are a way for webpages to maintain state and have the same information available for all pages within an application. They are not saved in a database and instead the session id is saved as part of a super-global variable in PHP called $_SESSION. This variable is then passed on to all requests for a web page by being stored inside the request header.
To create a session, the first thing that needs to be done is to call the function session_start() in your application before any output is sent to the browser. This will ensure that any remote client is given a unique session ID that will link up with their information on the server.
If you want to alter the name of a session, then you can do so using the function session_name() which sets the value of the Session cookie that the client’s browser will receive. You can also destroy a session entirely by calling the function session_destroy(). It doesn’t take an argument and a single call destroys all of the session data. This is much better than storing all of the session data on your system’s hard disk which is readable by any system user.