0% found this document useful (0 votes)
11 views16 pages

Practical12&13

The document outlines two experiments involving web development using PHP. Experiment 12 focuses on creating a web page with data validation, including form handling and error messaging, while Experiment 13 demonstrates session management and cookie handling for user authentication. Key components include form validation for various inputs, session management for logged-in users, and cookie usage for remembering user credentials.

Uploaded by

spandaa257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Practical12&13

The document outlines two experiments involving web development using PHP. Experiment 12 focuses on creating a web page with data validation, including form handling and error messaging, while Experiment 13 demonstrates session management and cookie handling for user authentication. Key components include form validation for various inputs, session management for logged-in users, and cookie usage for remembering user credentials.

Uploaded by

spandaa257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Experiment No: 12 and 13

Title of  Develop web page with data validation.


Experiment  Write simple PHP program to –
a. Set cookies and read it.
b. Demonstrate session management

1. Develop web page with data validation.

<?php include 'validation.php'; ?>

<!DOCTYPE html>
<html>
<head>
<title>PHP Form Validation with Reload</title>
</head>
<body style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f5f5f5; margin: 0; padding: 20px; display: flex; justify-content:
center; min-height: 100vh;">

<div style="background-color: white; padding: 30px; border-radius: 8px; box-shadow:


0 2px 10px rgba(0,0,0,0.1); width: 100%; max-width: 600px;">

<h2 style="color: #2c3e50; text-align: center; margin-bottom: 25px; border-bottom:


1px solid #eee; padding-bottom: 10px;">PHP Form Validation Example</h2>

<?php
if (isset($_SESSION['success'])) {
echo "<div style='background-color: #d4edda; color: #155724; padding: 10px;
border-radius: 4px; margin-bottom: 20px; text-align: center;'>" .
$_SESSION['success'] . "</div>";
unset($_SESSION['success']);
}
?>

<form method="post" enctype="multipart/form-data" action="index.php"


style="display: grid; gap: 15px;">

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Name:</label>
<input type="text" name="name" value="<?php echo $name; ?>"
style="padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$nameErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">E-mail:</label>
<input type="text" name="email" value="<?php echo $email; ?>"
style="padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$emailErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Date of Birth:</label>
<input type="date" name="dob" value="<?php echo $dob; ?>" style="padding:
8px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$dobErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Aadhar ID:</label>
<input type="text" name="aadhar" value="<?php echo $aadhar; ?>"
style="padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$aadharErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Gender:</label>
<div style="display: flex; gap: 15px;">
<label style="display: flex; align-items: center; gap: 5px;">
<input type="radio" name="gender" value="female" <?php if
($gender=="female") echo "checked";?>> Female
</label>
<label style="display: flex; align-items: center; gap: 5px;">
<input type="radio" name="gender" value="male" <?php if
($gender=="male") echo "checked";?>> Male
</label>
</div>
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$genderErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Country:</label>
<select name="country" style="padding: 8px; border: 1px solid #ddd; border-
radius: 4px; font-size: 14px;">
<option value="">Select</option>
<option value="USA" <?php if ($country == "USA") echo "selected";
?>>USA</option>
<option value="India" <?php if ($country == "India") echo "selected";
?>>India</option>
</select>
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$countryErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Hobbies:</label>
<div style="display: flex; gap: 15px;">
<label style="display: flex; align-items: center; gap: 5px;">
<input type="checkbox" name="hobbies[]" value="Reading" <?php if
(in_array("Reading", $hobbies)) echo "checked"; ?>> Reading
</label>
<label style="display: flex; align-items: center; gap: 5px;">
<input type="checkbox" name="hobbies[]" value="Traveling" <?php if
(in_array("Traveling", $hobbies)) echo "checked"; ?>> Traveling
</label>
</div>
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$hobbiesErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Website:</label>
<input type="text" name="website" value="<?php echo $website;?>"
style="padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;"><?php echo
$websiteErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Password:</label>
<input type="password" name="password" style="padding: 8px; border: 1px
solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$passwordErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Confirm Password:</label>
<input type="password" name="confirm_password" style="padding: 8px;
border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$confirmPasswordErr;?></span>
</div>

<div style="display: grid; gap: 5px;">


<label style="font-weight: 500;">Profile Picture:</label>
<input type="file" name="profile_pic" style="padding: 5px 0;">
<span style="color: #e74c3c; font-size: 13px;">* <?php echo
$profilePicErr;?></span>
</div>

<button type="submit" style="background-color: #3498db; color: white; padding:


10px; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; transition:
background-color 0.3s; margin-top: 10px;"
onmouseover="this.style.backgroundColor='#2980b9'"
onmouseout="this.style.backgroundColor='#3498db'">
Submit
</button>
</form>

</div>
</body>
</html>

Validation.php

<?php
session_start(); // Start session for success message

// Define variables and initialize with empty values


$name = $email = $gender = $website = $password = $confirmPassword = "";
$country = $dob = $aadhar = $profilePic = "";
$nameErr = $emailErr = $genderErr = $websiteErr = $passwordErr =
$confirmPasswordErr = "";
$countryErr = $dobErr = $aadharErr = $profilePicErr = $hobbiesErr = "";
$hobbies = []; // To store selected checkboxes
$successMsg = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$valid = true; // Flag to check if all inputs are valid

// Function to sanitize input


function test_input($data) {
return htmlspecialchars(stripslashes(trim($data)));
}

// Name Validation
if (empty($_POST["name"])) {
$nameErr = "Name is required";
$valid = false;
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z-' ]*$/", $name)) {
$nameErr = "Only letters and white space allowed";
$valid = false;
}
}

// Email Validation
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$valid = false;
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$valid = false;
}
}

// Password Validation
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
$valid = false;
} else {
$password = test_input($_POST["password"]);
if (strlen($password) > 8 && !preg_match("/[0-9]/", $password) &&
!preg_match("/[A-Z]/", $password) && !preg_match("/[\W]/", $password)) {
$passwordErr = "Password must be at least 8 characters long, include a
number, a capital letter, and a special character.";
$valid = false;
}
}

// Confirm Password Validation


if (empty($_POST["confirm_password"])) {
$confirmPasswordErr = "Please confirm your password";
$valid = false;
} else {
$confirmPassword = test_input($_POST["confirm_password"]);
if ($confirmPassword !== $password) {
$confirmPasswordErr = "Passwords do not match";
$valid = false;
}
}

// Website Validation
if (!empty($_POST["website"])) {
$website = test_input($_POST["website"]);
if (!preg_match("/^(http:\/\/|https:\/\/)/", $website)) {
$website = "http://" . $website; // Add http:// if missing
}
if (!filter_var($website, FILTER_VALIDATE_URL)) {
$websiteErr = "Invalid URL";
$valid = false;
}
}

// Gender Validation
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
$valid = false;
} else {
$gender = test_input($_POST["gender"]);
}

// Aadhar ID Validation
if (empty($_POST["aadhar"])) {
$aadharErr = "Invalid Aadhaar Number";
$valid = false;
} else {
$aadhar = test_input($_POST["aadhar"]);
if (!preg_match("/^[2-9]{1}[0-9]{11}$/", $aadhar)) {
$aadharErr = "Invalid Aadhaar Number";
$valid = false;
}
}

// Country Dropdown Validation


if (empty($_POST["country"])) {
$countryErr = "Please select a country";
$valid = false;
} else {
$country = test_input($_POST["country"]);
}

// Hobbies (Checkbox) Validation


if (!empty($_POST["hobbies"])) {
$hobbies = $_POST["hobbies"]; // Store selected checkboxes
} else {
$hobbiesErr = "Select at least one hobby";
$valid = false;
}

// Date of Birth (DOB) Validation


if (empty($_POST["dob"])) {
$dobErr = "Date of birth is required";
$valid = false;
} else {
$dob = $_POST["dob"];
$dobTimestamp = strtotime($dob);
$age = (date("Y") - date("Y", $dobTimestamp));

if ($age < 18) {


$dobErr = "You must be at least 18 years old";
$valid = false;
}
}

// File Upload (Profile Picture) Validation


if (!empty($_FILES["profile_pic"]["name"])) {
$allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];
$fileName = $_FILES["profile_pic"]["name"];
$fileSize = $_FILES["profile_pic"]["size"];
$fileTmpName = $_FILES["profile_pic"]["tmp_name"];
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

if (!in_array($fileExt, $allowedTypes)) {
$profilePicErr = "Only JPG, JPEG, PNG, and GIF files are allowed";
$valid = false;
} elseif ($fileSize > 2 * 1024 * 1024) { // Limit 2MB
$profilePicErr = "File size must be less than 2MB";
$valid = false;
} else {
move_uploaded_file($fileTmpName, "uploads/" . $fileName);
$profilePic = "uploads/" . $fileName;
}
} else {
$profilePicErr = "Profile picture is required";
$valid = false;
}

// If all inputs are valid, reload form with success message


if ($valid) {
$_SESSION['success'] = "Data submitted successfully!";
header("Location: index.php");
exit();
}
}
?>
Output:
2. Set cookies and read it.
 Dashboard.php

<?php
session_start();
if (!isset($_SESSION["is_logged_in"]) && isset($_COOKIE["username"])) {
$_SESSION["username"] = $_COOKIE["username"];
$_SESSION["is_logged_in"] = true;
}
if (!isset($_SESSION["is_logged_in"])) {
header("Location: login.php");
exit();
}

echo "Welcome, " . $_SESSION["username"] . "! <br>";


echo '<a href="logout.php">Logout</a>';
?>

Login.php

<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];

if ($username == "Sh" && $password == "Sh") {


$_SESSION["username"] = $username;
$_SESSION["is_logged_in"] = true;
if (!empty($_POST["remember"])) {
setcookie("username", $username, time() + (7 * 24 * 60 * 60), "/");
}

header("Location: index.php");
exit();
} else {
echo "Invalid login!";
}
}
?>

<form method="post">
<p> Username: <input type="text" name="username"><br><br></p>
<p> Password: <input type="password" name="password"><br><br></p>
<p> <input type="checkbox" name="remember"> Remember Me<br><br></p>
<p> <input type="submit" value="Login"><br></p>
</form>

Logout.php

<?php
session_start();
session_unset();
session_destroy();

setcookie("username", "", time() - 3600, "/");

header("Location: login.php");
exit();
?>

Output:
3. Demonstrate session management

Dashboard.php

<?php
session_start();

if (!isset($_SESSION["is_logged_in"])) {
header("Location: login.php");
exit();
}

echo "Welcome, " . $_SESSION["username"] . "! <br>";


echo '<a href="logout.php">Logout</a>';
?>

Login.php

<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
if ($username == "nova" && $password == "nova") {
$_SESSION["username"] = $username;
$_SESSION["is_logged_in"] = true;
header("Location: index.php");
exit();
} else {
echo "Invalid login credentials!";
}
}
?>
<form method="post">
<p> Username: <input type="text" name="username"><br><br></p>
<p> Password: <input type="password" name="password"><br><br></p>
<p> <input type="submit" value="Login"><br></p>
</form>

Logout.php

<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit();
?>

Output:
Grade and Process Product Dated Sign
Dated Related Related
Signature (35) (15)
of Teacher

You might also like