Cookie and Session example
A simple PHP project that demonstrates both cookies and sessions. This project consist of a user
registration and login system where cookies are used to remember the user's login status, and
sessions are used to maintain the user's state after logging in.
Project Structure
/cookie_session_demo
├── index.php
├── register.php
├── login.php
├── dashboard.php
└── logout.php
1. index.php - Home Page
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page</title>
</head>
<body>
<h1>Welcome to Cookie & Session Demo</h1>
<?php if (isset($_SESSION['username'])): ?>
<p>Hello, <?php echo $_SESSION['username']; ?>!</p>
<p><a href="dashboard.php">Go to Dashboard</a></p>
<p><a href="logout.php">Logout</a></p>
<?php else: ?>
<p><a href="register.php">Register</a></p>
<p><a href="login.php">Login</a></p>
<?php endif; ?>
</body>
</html>
2. register.php - User Registration
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// In a real application, you'd save this to a database
$_SESSION['username'] = htmlspecialchars($_POST['username']);
setcookie("username", $_SESSION['username'], time() + 3600, "/"); // 1 hour cookie
header("Location: dashboard.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<input type="submit" value="Register">
</form>
</body>
</html>
3. login.php - User Login
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// In a real application, you would validate against a database
$_SESSION['username'] = htmlspecialchars($_POST['username']);
setcookie("username", $_SESSION['username'], time() + 3600, "/"); // 1 hour cookie
header("Location: dashboard.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<input type="submit" value="Login">
</form>
</body>
</html>
4. dashboard.php - User Dashboard
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
</head>
<body>
<h1>Dashboard</h1>
<p>Hello, <?php echo $_SESSION['username']; ?>!</p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
5. logout.php - User Logout
<?php
session_start();
// Unset all of the session variables
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session
session_destroy();
echo "You have been logged out. <a href='index.php'>Return to Home</a>";
?>
How to Set Up
1. Create a Directory: Create a folder named cookie_session_demo in your web server's root
directory.
2. Create Files: Inside this folder, create the five PHP files as shown in the project structure.
3. Run the Server: Use a local server like XAMPP, WAMP, or MAMP to serve the project.
4. Access the Project: Open a web browser and go to
http://localhost/cookie_session_demo/index.php.
Explanation
Cookies: When a user registers or logs in, a cookie is set to remember their username for
one hour.
Sessions: The session is used to track the user's login state across different pages.
Logout: The user can log out, which destroys the session and the cookie.