WEB DEVELOPMENT USING PHP (4341604)
PRACTICAL 8:
Exercise 1.Write a PHP script to explain the concept of $_REQUEST.
Input:
<!DOCTYPE html>
<html>
<head>
<title>PHP $_REQUEST Example</title>
</head>
<body>
<h2>Using $_REQUEST to Get Form Data</h2>
<form method="post" action="">
Name: <input type="text" name="name"><br><br>
Email: <input type="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Using $_REQUEST to get form data
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
if (!empty($name) && !empty($email)) {
echo "<h3>Received Data:</h3>";
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "Email: " . htmlspecialchars($email);
} else {
echo "<h3 style='color:red;'>Please enter both Name and Email!</h3>";
}
}
?>
</body>
</html>
Output:
Received Data:
Name: John Doe
Email: johndoe@example.com
Exercise 2.Write a PHP script to demonstrate creating, deleting, updating, retrieving and
passing variable cookie data.
Input:
<?php
// Set a cookie (Create)
$cookie_name = "user";
$cookie_value = "Xyz";
setcookie($cookie_name, $cookie_value, time() + (86400 * 7), "/"); // 7 days expiry
// Update the cookie by setting a new value
if (isset($_COOKIE[$cookie_name])) {
setcookie($cookie_name, "UpdatedUser", time() + (86400 * 7), "/");
}
// Delete the cookie by setting expiry in the past
if (isset($_GET['delete'])) {
setcookie($cookie_name, "", time() - 3600, "/"); // Expired
header("Location: cookie_example.php"); // Refresh the page
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Cookie Example</title>
</head>
<body>
<h2>Cookie Demonstration</h2>
<?php
// Retrieve and display cookie data
if (isset($_COOKIE[$cookie_name])) {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value: " . $_COOKIE[$cookie_name] . "<br>";
} else {
echo "Cookie is not set.<br>";
}
?>
<!-- Button to delete the cookie -->
<form method="get">
<button type="submit" name="delete" value="1">Delete Cookie</button>
</form>
</body>
</html>
Output:
Cookie 'user' is set!
Value: UpdatedUser