Lecture Notes On Class 14: Sessions and Cookies

Rashmi Mishra
0

Lecture Notes On Class 14: Sessions and Cookies

Objective:

  • Understand the use of sessions and cookies in PHP.
  • Learn to manage session data and cookies for tracking user information.

Outcome:

Students will be able to:

  • Start sessions, store and retrieve session data.
  • Manage cookies to maintain user state.

Introduction

When developing web applications, tracking user information is crucial for providing personalized experiences. Two common methods for doing this in PHP are sessions and cookies.

What are Sessions?

A session in PHP is a way to store information (in variables) to be used across multiple pages. Unlike cookies, session data is stored on the server, making it more secure. Sessions are often used to store user information, such as login status or preferences, throughout a user's visit to a website.

What are Cookies?

Cookies are small files stored on the user’s computer by the web browser. They can be used to remember user preferences, login status, and other small pieces of information across sessions. Cookies have an expiration date, meaning they can persist even after the browser is closed.

1. Sessions in PHP

1.1 Starting a Session

To use sessions in PHP, you need to start the session at the beginning of your script using the session_start() function. This function must be called before any output is sent to the browser.

php

Copy code

<?php

// Start the session

session_start();

?>

1.2 Storing Session Data

You can store session data by assigning values to the $_SESSION superglobal array.

php

Copy code

<?php

session_start();

$_SESSION['username'] = 'JohnDoe'; // Store username in session

$_SESSION['email'] = 'john@example.com'; // Store email in session

?>

1.3 Retrieving Session Data

To retrieve session data, simply access the $_SESSION variable.

php

Copy code

<?php

session_start();

echo 'Welcome, ' . $_SESSION['username']; // Outputs: Welcome, JohnDoe

?>

1.4 Destroying a Session

To end a session and remove all session data, use the session_destroy() function. Before calling this function, it’s common to unset individual session variables as well.

php

Copy code

<?php

session_start();

session_unset(); // Unset all session variables

session_destroy(); // Destroy the session

?>


2. Cookies in PHP

2.1 Creating Cookies

You can set a cookie using the setcookie() function. This function must also be called before any output is sent to the browser.

php

Copy code

<?php

setcookie('user', 'JohnDoe', time() + (86400 * 30), "/"); // Cookie expires in 30 days

?>

  • Parameters:
    • name: The name of the cookie.
    • value: The value of the cookie.
    • expiration: The time the cookie expires (in seconds).
    • path: The path on the server where the cookie will be available.

2.2 Accessing Cookies

You can access cookies using the $_COOKIE superglobal array.

php

Copy code

<?php

if (isset($_COOKIE['user'])) {

    echo 'User: ' . $_COOKIE['user']; // Outputs: User: JohnDoe

}

?>

2.3 Deleting Cookies

To delete a cookie, set its expiration time to a past time.

php

Copy code

<?php

setcookie('user', '', time() - 3600, "/"); // Delete the cookie

?>


Comparison of Sessions and Cookies

Feature

Sessions

Cookies

Storage

Server-side

Client-side (browser)

Security

More secure

Less secure

Expiration

Ends when the session is destroyed

Can have specific expiration dates

Size Limit

No specific limit (server dependent)

Typically limited to 4KB per cookie


Conclusion

In this class, we covered the fundamentals of sessions and cookies in PHP. You learned how to start sessions, store and retrieve session data, and manage cookies to maintain user state. Understanding these concepts is essential for creating dynamic and personalized web applications.


Assignments

  1. Create a simple PHP application that utilizes sessions to track user login status.
  2. Implement cookies to remember user preferences, such as theme selection (light/dark mode).

Post a Comment

0Comments

Post a Comment (0)