PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

In PHP, sessions and cookies are used to store data across different pages of a web application. They are essential for maintaining state and data persistence in web applications. Let’s dive into each concept in detail:

Cookies

What are Cookies?

Cookies are small pieces of data stored on the client-side (usually in the browser). They are sent to the server with every request from the client and can be used to store user-specific information such as preferences, login status, and other data that should persist across sessions or visits.

How Cookies Work

  1. Set a Cookie: The server sends a Set-Cookie header in the HTTP response.
  2. Store the Cookie: The client’s browser stores the cookie.
  3. Send the Cookie: The browser includes the cookie in subsequent requests to the same server.

Creating and Using Cookies in PHP

				
					// Setting a cookie
setcookie("username", "JohnDoe", time() + (86400 * 30), "/"); // Expires in 30 days

// Retrieving a cookie
if(isset($_COOKIE["username"])) {
    echo "User is " . $_COOKIE["username"];
} else {
    echo "User is not set";
}

// Deleting a cookie
setcookie("username", "", time() - 3600, "/"); // Setting the expiration time to the past deletes the cookie

				
			

Cookie Parameters

  • name: The name of the cookie.
  • value: The value of the cookie.
  • expire: The time the cookie expires (given in Unix timestamp).
  • path: The path on the server in which the cookie will be available.
  • domain: The (sub)domain that the cookie is available to.
  • secure: Indicates if the cookie should only be sent over a secure HTTPS connection.
  • httponly: When true, the cookie will be accessible only through the HTTP protocol (not JavaScript).

Advantages and Disadvantages of Cookies

Advantages:

  • Simple to implement.
  • Can store small amounts of data on the client-side.
  • Persistent storage across sessions.

Disadvantages:

  • Limited in size (typically up to 4KB).
  • Stored on the client-side, so they can be manipulated or stolen.
  • Sent with every HTTP request, which can slightly impact performance.

Sessions

What are Sessions?

Sessions are used to store data on the server-side, allowing you to keep track of user data across multiple pages. A session ID is used to identify a user’s session and is usually stored in a cookie on the client-side or passed via URL.

How Sessions Work

  • Start a Session: The server creates a unique session ID and stores session data on the server.
  • Send Session ID: The session ID is sent to the client, typically stored in a cookie.
  • Session ID in Requests: The client sends the session ID with subsequent requests.
  • Access Session Data: The server retrieves session data using the session ID.

Creating and Using Sessions in PHP

				
					// Starting a session
session_start();

// Setting session variables
$_SESSION["username"] = "JohnDoe";
$_SESSION["email"] = "john.doe@example.com";

// Retrieving session variables
if(isset($_SESSION["username"])) {
    echo "Username is " . $_SESSION["username"];
}

// Unsetting session variables
unset($_SESSION["username"]);

// Destroying a session
session_destroy();

				
			

Advantages and Disadvantages of Sessions

Advantages:

  • Stores data on the server-side, which is more secure.
  • Can store larger amounts of data compared to cookies.
  • Ideal for sensitive information (e.g., login status).

Disadvantages:

  • Requires server resources to store session data.
  • Sessions are not persistent by default and typically expire after a set time.

Session Management in PHP

  • Session Lifetime: You can configure the session lifetime using php.ini settings or directly in your script.
				
					ini_set('session.gc_maxlifetime', 3600); // Set session lifetime to 1 hour
session_set_cookie_params(3600); // Set the cookie lifetime to 1 hour
session_start();

				
			
  • Session Storage: Sessions are stored in files by default, but you can configure them to be stored in databases or other storage mechanisms.

Summary

  • Cookies are used to store small pieces of data on the client-side, suitable for non-sensitive information that needs to persist across sessions.
  • Sessions are used to store larger and more sensitive data on the server-side, suitable for maintaining user state and data across multiple pages in a more secure manner.

Understanding the differences and appropriate use cases for cookies and sessions will help you design more secure and efficient web applications in PHP.

Scroll to Top