PHP Sessions


What is PHP sessions and how it works

PHP sessions are used briefly (until the user exits the website) to store and transfer details from one domain to another. PHP sessions technique is commonly used in shopping websites where we need to store and transfer on cart details from one platform to another, e.g. username, commodity code, commodity description, product price, etc. To remember the user and prevent a confrontation between different users, the PHP session generates special user ids for each client.

While you can use cookies to store info, it does have some security issues. Since cookies are saved on the user’s computer, an intruder may easily change the content of a cookie to inject potentially dangerous data into your application which may destroy your application.

Every when a client asks the server for a URL, all the cookie data for a website is immediately submitted to the server inside the message. This ensures that if you have placed 5 cookies on the user’s device, each of which has a size of 4 KB, the client has to upload 20 KB of data any time the user opens a page and may impact the output of your web.

Using the PHP session you will fix all of these problems. A PHP session, instead of the user’s device, stores data on the server. Every consumer is recognized in a session-based setting through a specific number called the session identifier or SID. This specific session ID serves to connect each user to their own server content, such as emails, blogs, etc.

You open it while you are dealing with a query, make any adjustments, and then you lock it. It’s sort of like a Meeting. The pc understands who you are. It knows when you start and when you finish the form. Yet there is one issue on the internet: the site server may not recognize who you are or what you are doing, as the HTTP address does not establish a state of affairs. Application variables solve this problem by storing multiple-use user knowledge. Session variables run by default before the consumer exits the window. And Session variables contain details for a particular person and are accessible in one program on all sites.

How to start the session in PHP

To launch the session the feature PHP session_start() is used. It starts a new session or completes a current one. When the session is already generated it restores the current session. If the session is not open, a new session is generated and returned.

Within your document the session_start() function must be the very first thing to do.

Session variables are contained in the so-named $_SESSION[] a super global variable. Such factors may be modified during a session’s lifespan.


bool session_start ( void )



PHP Sessions

How to store and access session data

You may store all of the session data in the $_SESSION[] associative array as key-value pairs. The collected data can be retrieved during a session’s lifespan. Consider the script below, which generates a new session, and documents two session variables.


$_SESSION[“user”] = “WebAlligator”;

How to destroy the session

Simply call the session_destroy() function to absolutely destroy a session though. This function does not require any argument, and all the session data is destroyed by a single request. In the illustration above we could see how we can use a single statement to destroy the whole session.