9th To 15th PHP Prgrms
9th To 15th PHP Prgrms
Description:
1.register.php: Displays a simple form with fields for name, email, and course. Once the user fills it out
and submits, the data is sent to submit.php.
2.submit.php: Receives the form data and prints it out on the webpage.
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Registration</title>
</head>
<body>
<label for="name">Name:</label>
<label for="email">Email:</label>
<label for="age">Age:</label>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$age = intval($_POST['age']);
$errors = [];
if (empty($name)) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
if ($age < 1) {
if (count($errors) > 0) {
echo "<p>$error</p>";
} else
} else {
?>
Output:
Description:
1.Complaint_form.php: This file contains a simple form that requests the user’s name, email, and
complaint details. It sends the data to submit_complaint.php when submites.
2.Submit_complaint.php: This script receives the submitted data and displays it in the page.
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Complaint Registration</title>
</head>
<body>
</form>
</body>
</html>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$complaint = $_POST['complaint'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Complaint Details</title>
</head>
<body>
</body>
</html>
Output:
Description
2. Function divide():
3. Try-Catch Block:
o The catch blocks handle different types of exceptions, providing specific error messages.
4. Finally Block:
o The finally block ensures that "Execution complete." is printed, regardless of whether an
exception occurred.
Program:
<?php
if (!is_numeric($numerator) || !is_numeric($denominator)) {
if ($denominator == 0) {
try {
$numerator = 10;
$denominator = 0;
} finally {
Output:
?>
Description
· Sending emails: Using PHPMailer is a better option than PHP's built-in mail() function as it provides
more features, security, and reliability.
· Receiving emails: Use the IMAP protocol with the PHP IMAP extension to fetch and read emails from a
mail server.
<?php
$to = '[email protected]';
'X-Mailer:PHP/'.phpversion();
else
?>
2. Basic Code for Receiving Emails Using IMAP (server configuration needed):
<?php
$hostname = '{imap.example.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'your-email-password';
$emails = imap_search($inbox,'ALL');
if ($emails)
$latest_email = max($emails);
$overview = imap_fetch_overview($inbox,$latest_email,0);
$message = imap_fetchbody($inbox,$latest_email,1);
}
else
imap_close($inbox);
?>
13. Construct a PHP Program to maintain the number of visits of the user using Cookies.
Description
· Cookie Check: The script checks if the visit_count cookie exists. If it does, the value is incremented. If
not, it starts with 1.
· Setting the Cookie: The setcookie() function is used to set/update the cookie with the current visit
count. The expiration is set to 30 days.
· Output: The program outputs a welcome message and the number of times the user has visited the
page.
Program:
<?php
if(isset($_COOKIE['user_visits']))
$visits = $_COOKIE['user_visits'] + 1;
echo "Welcome back! You have visited this page $visits times.";
else
$visits = 1;
?>
Output:
14. Construct a simple online shopping cart application using PHP. Using sessions, maintain a user's
shopping cart across multiple pages, even if they close the browser and return later.
Description
· Products Page: Display available products with the option to add them to the cart.
· Cart Page: Display the items added to the cart, with options to remove items or update quantities.
· Session Management: Use PHP sessions to maintain the cart state across multiple pages and store the
cart data even if the user leaves and returns later (by using sessions combined with cookies).
<?php
session_start();
if (!isset($_SESSION['cart']))
$_SESSION['cart'] = [];
if (isset($_POST['add']))
$product = $_POST['product'];
$_SESSION['cart'][] = $product;
}
?>
<h1>Products</h1>
<form method="post">
<ul>
<li>
</li>
</ul>
</form>
<?php
session_start();
if (isset($_POST['clear']))
$_SESSION['cart'] = [];
?>
<h1>Your Cart</h1>
<ul>
</ul>
<form method="post">
</form>
Output:
Introspection in PHP refers to the ability of the program to inspect classes, objects, and interfaces to find
out what properties and methods they have at runtime.
Serialization is the process of converting an object into a string representation so that it can be stored or
transmitted, and then later reconstructed back into its original object form.
Program
<?php
$this->brand = $brand;
$this->model = $model;
echo "Does displayInfo method exist? " . (method_exists($myCar, 'displayInfo') ? 'Yes' : 'No') . "<br>";
$serializedCar = serialize($myCar);
$unserializedCar = unserialize($serializedCar);
?>