Lab Session 09 WebEngineering
Lab Session 09 WebEngineering
Department of CS and SE
Exercise
1. Create a cookie named 'theme' that stores the user's preferred website theme. Allow the user to
select a theme from a dropdown menu and save it as a cookie. Retrieve the theme from the cookie
and apply it to the website layout.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Theme Selector</title>
</head>
<body>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the selected theme from the form
if (isset($_POST['theme'])) {
$selectedTheme = $_POST['theme'];
Department of CS and SE
</div>
<script>
// Retrieve the theme cookie value
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
<style>
/* Define styles for different themes */
body {
font-family: Arial, sans-serif;
transition: background-color 0.3s ease;
align-items: center;
justify-content: center;
text-align: center;
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 4px;
outline: none;
Web Design and Development
Department of CS and SE
button:hover {
background-color: #2980b9;
}
select {
padding: 10px;
font-size: 16px;
width: 100%;
border: 1px solid #ddd;
border-radius: 4px;
outline: none;
transition: border-color 0.3s ease;
}
.container {
color:black;
text-align: center;
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;}
.light {
background-color: #f9f9f9;
color: #333;
}
.dark {
background-color: #333;
color: #f9f9f9;
}
.blue {
background-color: #3498db;
color: #fff;
}
</style>
Web Design and Development
Department of CS and SE
</body>
</html>
Output:
Light theme:
Dark Theme:
Web Design and Development
Department of CS and SE
Blue Theme:
2. Implement a login system using PHP sessions. Create a login form that accepts a username and
password. On successful login, store the user's ID and username in session variables. Restrict
access to certain pages only to authenticated users by checking the session variables.
SignUp Page:
<?php
Web Design and Development
Department of CS and SE
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inventory";
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Registration</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;
Web Design and Development
Department of CS and SE
h2 {
color: #333;
margin-bottom: 20px;
}
p {
color: #666;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 4px;
outline: none;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<div class="container">
<h2>Register</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?
>">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Register</button>
<p>Already have an account? <a href="login.php">Login here</a></p>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
Web Design and Development
Department of CS and SE
<?php
$conn->close();
?>
OUTPUT:
Web Design and Development
Department of CS and SE
LOGIN PAGE:
<?php
session_start();
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inventory";
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Login</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
Web Design and Development
Department of CS and SE
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;
}
h2 {
color: #333;
margin-bottom: 20px;
}
p {
color: #666;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 4px;
outline: none;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
Department of CS and SE
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if (password_verify($password, $row['password'])) {
// Authentication successful, set session variables
$_SESSION['user_id'] = $row['id'];
$_SESSION['username'] = $row['username'];
<?php
$conn->close();
?>
OUTPUT:
Web Design and Development
Department of CS and SE
<?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>Interactive Blog Page</title>
Web Design and Development
Department of CS and SE
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 24px;
font-weight: bold;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline-block;
margin-right: 20px;
}
nav ul li a {
color: #fff;
text-decoration: none;
transition: color 0.3s ease;
}
nav ul li a:hover {
color: #ffd700;
}
Web Design and Development
Department of CS and SE
main {
padding: 20px;
display: flex;
justify-content: space-between;
}
#content {
flex-grow: 2;
}
.blog-post {
background-color: #fff;
padding: 20px;
margin-bottom: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.blog-post:hover {
transform: scale(1.03);
}
aside {
width: 250px;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
#search {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
transition: border-color 0.3s ease;
}
#search:focus {
Web Design and Development
Department of CS and SE
border-color: #007bff;
}
button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<header>
<div class="logo"><b><span style="color: #007bff;"><i>Write</i></span>
Wise</b></div>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
<li><a href="logout.php">LogOut</a></li>
</ul>
</nav>
</header>
<main>
<section id="content">
<div class="blog-post">
<h2 ><a href="#">Data Domain System</a></h2>
<p>Date: May 1, 2024</p>
<p>Summary: The article explains how the Domain Name System (DNS)
provides a human-friendly way to identify websites on the Internet. Instead of
Web Design and Development
Department of CS and SE
<aside style="height:50%;">
<input type="text" id="search" placeholder="Search...">
<button id="filterButton">Filter</button>
</aside>
</main>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// Fade in the header section
Web Design and Development
Department of CS and SE
$('header').fadeIn('slow');
Department of CS and SE
</body>
</html>
OUTPUT:
LOGOUT:
<?php
// Initialize session
session_start();
Department of CS and SE
Department of CS and SE
3. Add validation to the cookie creation process. Ensure that the cookie value meets certain criteria,
such as being alphanumeric and having a minimum length. Display an error message if the cookie
value is invalid.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cookie Validation</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.error {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h2>Cookie Validation</h2>
<?php
// Check if form is submitted
if (isset($_POST['submit'])) {
$cookie_value = $_POST['cookie_value'];
Department of CS and SE
OUTPUT:
Web Design and Development
Department of CS and SE
4. Implement a session timeout mechanism. Set an expiration time for the session and automatically
log out the user when the session expires. Display a countdown timer to notify the user before the
session timeout occurs.
CODE:
<?php
// Set session expiration time to 30 minutes (1800 seconds)
$session_lifetime = 1800; // 30 minutes in seconds
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Session Timeout</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
</style>
<script>
// Countdown timer function
Web Design and Development
Department of CS and SE
if (--timer < 0) {
// Session expired, redirect user to logout page
window.location.href = "logout.php";
}
}, 1000);
}
OUTPUT:
Web Design and Development
Department of CS and SE