Creating, retrieving, and deleting cookies
Managing user preferences with cookies
Objective:
The objective of this lesson is to introduce learners to the concept of cookies in PHP. By the end of this lesson, participants should be able to:
- Understand what cookies are and their role in web development.
- Create, retrieve, and delete cookies using PHP.
- Implement user preferences management using cookies.
Description:
2.1 Understanding Cookies
2.1.1 Introduction
Cookies are small pieces of data stored on the client’s browser, providing a way to persist information between web pages. In PHP, developers can create, retrieve, and delete cookies to enhance the user experience.
2.1.2 How Cookies Work
Explain the basic mechanism of how cookies work, including how they are sent from the server to the client and included in subsequent HTTP requests.
2.2 Creating, Retrieving, and Deleting Cookies
2.2.1 Creating Cookies
Demonstrate how to set a cookie in PHP using the setcookie()
function. Cookies can store information such as user preferences or session data.
<?php
// Example of creating a cookie
setcookie('user_id', '123', time() + 3600, '/');
?>
2.2.2 Retrieving Cookies
Illustrate how to retrieve the value of a cookie using the $_COOKIE
superglobal.
<?php
// Example of retrieving a cookie
$userID = $_COOKIE['user_id'];
echo "User ID: " . $userID;
?>
2.2.3 Deleting Cookies
Explain how to delete a cookie by setting its expiration time to a past date.
<?php
// Example of deleting a cookie
setcookie('user_id', '', time() - 3600, '/');
?>
2.3 Managing User Preferences with Cookies
2.3.1 Storing User Preferences
Show how cookies can be used to store and retrieve user preferences, such as theme choices or language selections.
<?php
// Example of storing user preferences in a cookie
$theme = 'dark';
setcookie('user_theme', $theme, time() + 3600, '/');
?>
2.3.2 Retrieving User Preferences
Demonstrate how to retrieve and apply user preferences in subsequent page loads.
<?php
// Example of retrieving and applying user preferences
$userTheme = isset($_COOKIE['user_theme']) ? $_COOKIE['user_theme'] : 'default';
echo "Selected Theme: " . $userTheme;
?>
2.4 Best Practices and Security Considerations
2.4.1 Cookie Security
Discuss best practices for securing cookies, including using secure and HttpOnly flags, and avoiding sensitive data in cookies.
<?php
// Example of setting secure and HttpOnly flags
setcookie('user_id', '123', time() + 3600, '/', '', true, true);
?>
2.4.2 Cookie Expiry and Domain
Explain considerations for setting cookie expiry times and specifying the domain to manage cookie scope.
<?php
// Example of setting cookie expiry time and domain
setcookie('user_id', '123', time() + 3600, '/', 'example.com');
?>
2.5 Practice Exercise
Task:
- Create a User Preference Form:
- Develop an HTML form that allows users to select preferences such as language and theme.
- Process the form using PHP to set cookies with the selected preferences.
- Display User Preferences:
- Create a PHP page that retrieves and displays the user’s stored preferences.
- Update User Preferences:
- Extend the form to allow users to update their preferences.
- Implement the PHP logic to update the corresponding cookies.
- Logout and Clear Preferences:
- Create a logout feature that deletes relevant cookies and redirects users to the login page.
Create a User Preference Form:
- Develop an HTML form named
preference_form.html
that allows users to select preferences such as language and theme. - Submit the form to a PHP script for processing.
<!-- preference_form.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Preference Form</title>
</head>
<body>
<form action="process_preferences.php" method="post">
<label for="language">Language:</label>
<select name="language">
<option value="english">English</option>
<option value="spanish">Spanish</option>
<option value="french">French</option>
</select>
<label for="theme">Theme:</label>
<select name="theme">
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
<button type="submit">Save Preferences</button>
</form>
</body>
</html>
Process User Preferences:
- Create a PHP script named
process_preferences.php
to handle the form submission. - Set cookies with the selected preferences.
<?php
// process_preferences.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$language = $_POST['language'] ?? 'english';
$theme = $_POST['theme'] ?? 'light';
// Set cookies with selected preferences
setcookie('user_language', $language, time() + 3600, '/');
setcookie('user_theme', $theme, time() + 3600, '/');
header('Location: display_preferences.php');
exit();
}
?>
Display User Preferences:
- Create a PHP page named
display_preferences.php
to retrieve and display the user’s stored preferences.
<?php
// display_preferences.php
// Retrieve user preferences from cookies
$userLanguage = $_COOKIE['user_language'] ?? 'english';
$userTheme = $_COOKIE['user_theme'] ?? 'light';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Preferences</title>
</head>
<body>
<h1>User Preferences</h1>
<p>Language: <?php echo ucfirst($userLanguage); ?></p>
<p>Theme: <?php echo ucfirst($userTheme); ?></p>
<a href="preference_form.html">Update Preferences</a>
</body>
</html>
Logout and Clear Preferences:
- Create a PHP script named
logout.php
to handle user logout. - Delete relevant cookies and redirect users to the login page.
<?php
// logout.php
// Delete relevant cookies
setcookie('user_language', '', time() - 3600, '/');
setcookie('user_theme', '', time() - 3600, '/');
// Redirect to the login page
header('Location: login.html');
exit();
?>
Additional Notes:
- Make sure to validate and sanitize user input in the
process_preferences.php
script. - Consider adding additional security measures, such as using secure and HttpOnly flags for cookies.
This practice exercise allows learners to implement a practical scenario involving user preferences and cookie management in PHP.