Sessions in PHP With Example

Sessions in PHP With Example


Cookies are used to store user data on the client's browser is not the most secure way of storing data, it can be easily hacked.

Cookie data for a website is uploaded every time a request is sent for a specifying URL on a server. For example, 10 cookies of a site are stored on a client browser then for every request it has to upload 40KB of data for each request done from the server as one cookie is of 4KB.

Both this problem can be solved by using the session to store user data

Session data is stored on the server-side and each Session is assigned with a unique Session ID (SID) for that session data.

As session data is stored on the server there is no need to send any data along with the URL for each request to the server.

More data can be stored in session as compared with cookie because the location for storing data is a server.

PHP stores the session data in a temporary file on the server, the location of the temporary file is specified by the session.save_path directives in the PHP configuration file

PHP session is used to store and pass information from one page to another temporarily (until the user closes the website).

PHP session technique is widely used in shopping websites where we need to store and pass information

e.g. username, product code, product name, product price, etc from one page to another.

PHP session creates a unique user id for each browser to recognize the user and avoid conflict between multiple browsers.

Use of Session in PHP:



A session is used to store important information such as the user id more securely on the server where malicious users cannot tamper with them.

To pass values from one page to another.

Sessions are the alternative to cookies on browsers that do not support cookies.

You want to store global variables in an efficient and more secure way compared to passing them in the URL

You are developing an application such as a shopping cart that has to temporarily store information with a capacity larger than 4KB.

Start Session in PHP:

it very easy to create and start a session in PHP.

PHP session can be started by calling session_start() function.


If it is a new session then session_start() function will generate a unique SID for the session or else the same function will be used to access existing along with the data store in that session.

Set Session Variable in PHP

The session variable can be set with the help of a PHP global variable: $ SESSION.

Data in the session is stored in the form of keys and values pair.

We can store any type of data on the server, which includes arrays and objects. echo "User


For example, we want to store the username in the session so it can be assessed whenever it is required throughout the session.

Example of Session in PHP
<?
session_start();
?>
<!DOCTYPE html>
<html>
<head>
 <title>cwipedia.in</title>
</head>
<body>
<?
$_SESSION['Name']="Shivam";
$_SESSION['RollNo']="CW302";
echo "Session vars are set";
echo "<br>",$_SESSION['Name'];
echo "<br>",session_id();#Using this you can retrive unqiue session id 
?>
</body>
</html>

Comments