In this article Sessions in PHP we give the information about Sessions are a way to store user information on the server-side. It is used to store user information in a secure and encrypted form, so that the information does not have to be sent every time the user visits the browser.

Sessions and Cookies in PHP

Introduction

Sessions and cookies are essential features in web development that help improve user interaction and experience.
They are technical methods used to store and manage user data, allowing information to persist across multiple web pages within a website.

Sessions in PHP

What is a Session?

A session in PHP is a way to store user information on the server-side.
It allows data to remain available throughout a user’s interaction with a website without having to resend it every time the page is loaded.
Sessions are stored securely and are not directly visible to the user, which makes them more secure than cookies.

Session

How Sessions Work

Step 1: Start a Session

Use the session_start() function to begin a session.

session_start();

Step 2: Store Session Data

You can store data in a session using the $_SESSION superglobal variable.

$_SESSION[“name”] = “John”;

Step 3: Retrieve Session Data

Access session values using the same variable name.

echo $_SESSION[“name”];

Delete Session Data

To remove or destroy session data, the following functions are used:

Step 1: Remove All Session Variables

session_unset();

Step 2: Destroy the Session

session_destroy();

Example: Session in PHP

File: s1.php

<?php

session_start();

$_SESSION[“My_Hobbies”] = “Reading”;

echo “Session variable is set.”;

?>

Explanation:
Here, a session variable named “My_Hobbies” is created and assigned the value “Reading”. The information is stored on the server and can be accessed across pages during the session.

Cookies in PHP

What are Cookies?

A cookie in PHP is a small text file stored on the client’s computer by the web server.
Cookies help a website remember information about the user’s previous visits — such as login details, preferences, or language selection.
They enhance personalization and improve user experience.

Advantages of Cookies

  • Help identify returning users.
  • Simplify website navigation and personalization.
  • Enable tracking and analysis of web traffic patterns.

How to Set Cookies in PHP

Cookies are created using the setcookie() or setrawcookie() function.

Syntax:

setcookie(name, value, expire, path, domain, secure, httponly);

Parameters:

Parameter Description
name The name of the cookie.
value The data to store in the cookie.
expire Expiration time (in seconds).
path The path within the domain where the cookie is available.
domain The domain name for which the cookie is valid.
secure If true, cookie data is sent only over HTTPS.
httponly If true, the cookie cannot be accessed via JavaScript.

Example: Cookie in PHP

<?php

$cookie_name = “user”;

$cookie_value = “Easy Concept”;

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”);

?>

<html>

<body>

<?php

if(!isset($_COOKIE[$cookie_name])) {

echo “Cookie is not set.”;

} else {

echo “Cookie Value: ” . $_COOKIE[$cookie_name];

}

?>

</body>

</html>

Explanation:
A cookie named “user” is created and assigned the value “Easy Concept”. The cookie will expire after 30 days (86400 seconds × 30).

Difference Between Sessions and Cookies

Aspect Sessions Cookies
Storage Location Stored on the server-side Stored on the client-side
Security More secure (not visible to users) Less secure (accessible to users)
Data Lifetime Ends when the browser is closed Remains until the user clears browser data or until expiry
Performance Uses server memory (lightweight for users) Uses client storage (heavier for browsers)
Accessibility Accessible only on the server Accessible on both client and server

Using Sessions and Cookies Together

Developers often use sessions and cookies together to ensure that both the server and client can store and access user data effectively.

Example: Using Both Sessions and Cookies

<?php

// Start the session

session_start();

// Set a cookie

setcookie(“user”, “JohnDoe”, time() + (86400 * 30), “/”);

// Set a session variable

$_SESSION[‘username’] = “JohnDoe”;

// Retrieve and display data

echo “Hello, ” . $_SESSION[‘username’] . ” and ” . $_COOKIE[‘user’];

?>

Explanation:
Here, both a session and a cookie are created.

  • The session stores data on the server,
  • The cookie stores data on the client-side,
    allowing consistent data sharing between browser and server.

Conclusion

  • Sessions are used for secure server-side data storage and are ideal for storing sensitive information such as login credentials.
  • Cookies store client-side data, enabling a personalized user experience across visits.
  • Using both mechanisms together provides a balanced approach, allowing data to persist efficiently and securely across multiple web pages.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *