13) SESSION AND COOKIES
a) create, delete, modify, retrieve, check and destroy session opera ons
<!DOCTYPE html>
<html>
<head>
< tle>Session Opera ons</ tle>
</head>
<body>
<h2>Session Opera ons</h2>
<!-- Create/Modify Form -->
<form method="POST" ac on="">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="" required>
<label for="user_id">User ID:</label>
<input type="text" id="user_id" name="user_id" value="" required><br><br>
<bu on type="submit" name="create">Create/Modify Session</bu on><br><br>
</form>
<!-- Retrieve/Check Form -->
<form method="POST" ac on="">
<bu on type="submit" name="retrieve">Retrieve/Check Session</bu on><br><br>
</form>
<!-- Delete Form -->
<form method="POST" ac on="">
<bu on type="submit" name="delete">Delete Session</bu on><br><br>
</form>
<?php
session_start(); // Start the session
// Check if the form is submi ed
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Create or Modify Session
if (isset($_POST["create"])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['user_id'] = $_POST['user_id'];
echo "Session created/modified successfully!";
// Delete Session
elseif (isset($_POST["delete"])) {
// Unset and destroy session variables
session_unset();
session_destroy();
echo "Session deleted successfully!";
// Retrieve and Check Session
elseif (isset($_POST["retrieve"])) {
$username = isset($_SESSION['username']) ? $_SESSION['username'] : "Not set";
$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : "Not set";
echo "Username: $username<br><br>";
echo "User ID: $user_id";
?>
</body>
</html>
OUTPUT:
Create
Retrieve
Delete
b) start, get, modify, and destroy cookie variable
<!DOCTYPE html>
<html>
<head>
< tle>Cookie Opera ons</ tle>
</head>
<body>
<h2>Cookie Opera ons</h2>
<!-- Start Cookie Form -->
<form method="POST" ac on="">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<bu on type="submit" name="start">Start Cookie</bu on><br><br>
</form>
<!-- Get Cookie Form -->
<form method="POST" ac on="">
<bu on type="submit" name="get">Get Cookie</bu on><br><br>
</form>
<!-- Modify Cookie Form -->
<form method="POST" ac on="">
<label for="username">New Username:</label>
<input type="text" id="username" name="username" required><br><br>
<bu on type="submit" name="modify">Modify Cookie</bu on><br><br>
</form>
<!-- Destroy Cookie Form -->
<form method="POST" ac on="">
<bu on type="submit" name="destroy">Destroy Cookie</bu on>
</form><?php
// Start the session
session_start();
// Check if the form is submi ed
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Start Cookie
if (isset($_POST["start"])) {
$username = $_POST['username'];
// Set a cookie variable
setcookie("user", $username, me() + 3600, "/");
echo "Cookie started successfully!";
// Get Cookie
elseif (isset($_POST["get"])) {
// Retrieve cookie variable
$username = isset($_COOKIE['user']) ? $_COOKIE['user'] : "Not set";
echo "Username from cookie: $username";
// Modify Cookie
elseif (isset($_POST["modify"])) {
$username = $_POST['username'];
// Modify cookie variable
setcookie("user", $username, me() + 3600, "/");
echo "Cookie modified successfully!";
// Destroy Cookie
elseif (isset($_POST["destroy"])) {
// Unset and destroy cookie variable
setcookie("user", "", me() - 3600, "/");
echo "Cookie destroyed successfully!";
?>
</body>
</html>
OUTPUT:
Start:
Get:
Modify:
Destroy:
c)create login and logout form using session
<?php
session_start();
// Login Form
if(isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// Check if username and password are correct
if($username == 'aarthi' && $password == '123') {
$_SESSION['username'] = $username;
echo "Login successful!";
} else {
echo "Invalid username or password!";
// Logout Form
if(isset($_POST['logout'])) {
session_destroy();
echo "Logged out successfully!";
?>
<!DOCTYPE html>
<html>
<head>
< tle>Login and Logout Form</ tle>
</head>
<body>
<?php if(isset($_SESSION['username'])) { ?>
<h1>Welcome, <?php echo $_SESSION['username']; ?></h1>
<form method="post" ac on="">
<input type="submit" name="logout" value="Logout">
</form>
<?php } else { ?>
<h1>Login</h1>
<form method="post" ac on="">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<input type="submit" name="login" value="Login">
</form>
<?php } ?>
</body>
</html>
Output: