PHP File
PHP File
Aim: The aim of this practical exercise is to implement a web form using PHP
and Bootstrap, enabling data collection with validation, submission handling,
and displaying the submitted data in a formatted manner.
Program :
index.php
</head>
<body>
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#"> Bootstrap
</a>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search"
arialabel="Search">
<button class="btn btn-outline-success" type="submit">Search</button> </form>
</div>
</nav>
form_data.php
<!DOCTYPE html> <html
lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include "header.php";?>
</head> <body>
<?php
// print_r($_POST);
echo "<h1>Hello ". $_POST["fname"]." ".$_POST["lname"]." ! </h1><br/> ";
?>
<div class="card" style="width: 18rem;">
<div class="card-header">
<?php echo $_POST["user"]; ?>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"><?php echo $_POST["city"]; ?></li>
<li class="list-group-item"><?php echo $_POST["education"]; ?></li>
<li class="list-group-item"><?php echo $_POST["zipcode"]; ?></li> </ul>
</div>
<br/><br/><br/><br/>
<?php include "footer_include.php"; ?>
</body>
</html>
header.php
<head>
<title>PHP DEMO</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT"
crossorigin="anonymous">
</head>
footer_include.php
<footer>
© 2012 - <?php echo date("Y"); ?>
</footer> <script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min. js"
integrity="sha384-
u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8"
crossorigin="anonymous"></script>
PRACTICAL – 2
Aim: This exercise aims to build a basic login form utilizing HTML,
Bootstrap for styling, and PHP for form handling. Upon submission,
form_handler.php processes the data, displays a thank-you message, and
showcases the submitted information, including email,password,
and gender. Basic validation ensures mandatory fields are filled in.
Program: form1.php
<!DOCTYPE html>
<html>
<head>
<title>A Simple Login Form</title>
<?php include "header.php" ;?>
</head>
<body>
<form action="form_handler.php" method="get">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" name="exampleInputEmail1"
id="exampleInputEmail1" aria-describedby="emailHelp">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" name="exampleInputPassword1"
id="exampleInputPassword1">
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label> </div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault"
id="flexRadioDefault1" value="F">
<label class="form-check-label" for="flexRadioDefault1">
Female
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault"
id="flexRadioDefault2" value="M" checked>
<label class="form-check-label" for="flexRadioDefault2">
Male
</label>
</div>
</body>
</html>
form_handler.php
<!DOCTYPE html>
<html>
<head>
<title>Thank You</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
</head>
<body>
<h1>Thank You</h1>
<p>Thank you for registering. Here is the information you submitted:</p>
Program: connect.php
<?php
$servername = "localhost"; // Replace with your server name
$username = "root"; // Replace with your MySQL username
$password = ""; // Replace with your MySQL password
$database = "crud"; // Replace with your MySQL database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database, 3307);
if(!$conn){
//echo "connection successfull";
die(mysqli_error($conn));
echo "connection unsuccessfull";
}
?>
delete.php
<?php
include 'connect.php';
if(isset($_GET['deleterno'])){
$rno = $_GET['deleterno'];
if($result){
// echo 'Deleted Successfully!';
header('location:display.php');
} else {
die(mysqli_error($conn));
}
}
?>
display.php
<?php
include 'connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD Data</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<button class="btn btn-primary my-5">
<a href="user.php" class="text-light">Add User</a>
</button>
<table class="table">
<thead>
<tr>
<th>Student Name</th>
<th>Class</th>
<th>Student Number</th>
<th>Address</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
<?php
$sql = "select * from `db`";
$result = mysqli_query($conn, $sql);
if($result){
// $row = mysqli_fetch_assoc($result);
// echo $row['name'];
while($row = mysqli_fetch_assoc($result)){
$rollno = $row['rollno'];
$name = $row['name'];
$class = $row['class'];
$address = $row['address'];
echo '<tr>
<th scope="row">'.$rollno.'</th>
<td>'.$name.'</td>
<td>'.$class.'</td>
<td>'.$address.'</td>
<td>
<button class="btn btn-primary"><a href="update.php?updaterno='.$rollno.'" class="text-
light">Update</a></button>
<button class="btn btn-danger"><a href="delete.php?deleterno='.$rollno.'" class="text-
light">Delete</a></button>
</td>
</tr>';
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
update.php
<?php
include 'connect.php';
$rno = $_GET['updaterno'];
$sql = "select * from `db` where rollno=$rno";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$class = $row['class'];
$rollno = $row['rollno'];
$address = $row['address'];
if(isset($_POST['submit'])){
$name = $_POST['name'];
$class = $_POST['class'];
$rollno = $_POST['rollno'];
$address = $_POST['address'];
if($result){
// echo "Data Updated successfully...";
header('location:display.php');
} else {
die(mysqli_error($conn));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD operation</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-center">Welcome to student portal!</h1>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
user.php
<?php
include 'connect.php';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$class = $_POST['class'];
$rollno = $_POST['rollno'];
$address = $_POST['address'];
if($result){
//echo "Data inserted successfully...";
header('location:display.php');
} else {
die(mysqli_error($conn));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD operation</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-center">Welcome to student portal!</h1>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Db.sql
--
-- Database: `crud`
--
-- --------------------------------------------------------
--
-- Table structure for table `db`
--
--
-- Dumping data for table `db`
--
Aim: The aim of this project is to develop a basic web application for user
registration and authentication. It includes functionalities such as user sign-
up (sign.php), login (login.php), and logout (logout.php).
Program:
connect.php
<?php
$servername = "localhost"; // Replace with your server name
$username = "root"; // Replace with your MySQL username
$password = ""; // Replace with your MySQL password
$database = "emp"; // Replace with your MySQL database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database, 3307);
if(!$conn){
die(mysqli_error($conn)); // echo "connection successfull";
}
?>
Home.php
<?php
session_start();
if(!isset($_SESSION['empname'])){
header('location:login.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome Page</title>
</head>
<body>
<h1 class="text-center mt-5">Hello, <?php echo $_SESSION['empname']; ?></h1>
<div class="container">
<a href="logout.php" class="btn btn-primary mt-5">Logout</a>
</div>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"
crossorigin="anonymous"></script>
-->
</body>
</html>
login.php
<?php
$login = 0;
$invalid = 0;
//echo $user;
if($_SERVER['REQUEST_METHOD']=='POST'){
include 'connect.php';
$name = $_POST['empname'];
$pass = $_POST['emppass'];
if($result){
$num = mysqli_num_rows($result);
if($num>0){
// echo 'Login Successful';
$login = 1;
// create a session
session_start();
$_SESSION['empname'] = $name;
header('location:home.php');
} else {
// echo 'Invalid Data';
$invalid = 1;
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Page</title>
</head>
<body>
<?php
if($login){
echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Success!</strong> You have successfully login.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
}
?>
<?php
if($invalid){
echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error !</strong> Invalid Credentials.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
}
?>
<div class="container my-5">
<h1 class="text-center">Login to our Page </h1>
<form action="login.php" method="post">
<div class="mb-3">
<label class="form-label">User Name:</label>
<input type="text" class="form-control" name="empname" placeholder="Enter user
name:">
</div>
<div class="mb-3">
<label class="form-label">Password:</label>
<input type="password" class="form-control" name="emppass" placeholder="Enter your
Password" >
</div>
sign.php
<?php
$success = 0;
$user = 0;
//echo $user;
if($_SERVER['REQUEST_METHOD']=='POST'){
include 'connect.php';
$name = $_POST['empname'];
$pass = $_POST['emppass'];
if($result){
$num = mysqli_num_rows($result);
if($num>0){
// echo 'User already exists!';
$user = 1;
// echo $user;
} else {
$sql = "insert into `rst` (empname, emppass) values('$name', '$pass')";
$result = mysqli_query($conn, $sql);
if($result){
//echo "SignUp successfully!";
$success = 1;
$user = 0;
} else {
die(mysqli_error($conn));
}
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign Up Page</title>
</head>
<body>
<?php
if($success){
echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Successful !</strong> You have created account successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
}
?>
<?php
if($user){
echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Sorry !</strong> User Already exists...
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
}
?>
logout.php
<?php
session_start(); session_destroy();
header('location:login.php');
?>
Emp.sql
--
-- Database: `emp`
--
-- --------------------------------------------------------
--
-- Table structure for table `rst`
--
Aim: The aim of this project is to demonstrate the usage of cookies in PHP. It
involves creating a cookie named "Brave" with the value "Google" and then
retrieving and displaying the cookie's value on the webpage.
Program : cookie.php
<?php
// create cookie with PHP (must be before html tag)
$cookie_name = "Brave";
$cookie_value = "ABCD";
setcookie($cookie_name, $cookie_value, time() + (86400 * -1), "/" );
# to set and modify cookie use the same function.
# to delete the cookie usef same function with past date.
?>
<html>
<body>
<?php
include '03header.php';
if(!isset($_COOKIE[$cookie_name])){
echo "Cookie named " . $cookie_name . " is not set!";
} else {
echo "Cookie " . $cookie_name . " is set. <br>";
echo "Value is ". $_COOKIE[$cookie_name];
}
include '02footer_include.php';
?>
</body>
</html>
header.php
<head>
<title>PHP DEMO</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT"
crossorigin="anonymous">
</head>
02footer_include.php
<!-- To print automatically copyright year-->
<footer>
© 2012 - <?php echo date("Y"); ?>
</footer> <script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-
u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8"
crossorigin="anonymous"></script>
PRACTICAL – 6
Aim: The aim of this project is to illustrate the usage of sessions in PHP. It
starts by initiating a session, unsetting all session variables, and then
destroying the session. Subsequently, it sets session variables ("user" and
"pwd") and displays a message confirming their assignment. The project
employs PHP for session management and includes Bootstrap for basic
styling.
Program:
07session.php
<?php
// start a session
// it must be the very first thing in your document, before any html tags
session_start(); // imp functions echo session_id()."<br>"; echo
session_name(). "<br>"; // to remove all session variables
session_unset(); // to
destroy session
session_destroy();
?>
<!DOCTYPE html>
<html>
<?php include '03header.php';?>
<body>
<?php
// set session variables.
$_SESSION["user"] = "Brave";
$_SESSION["pwd"] = "Pass"; echo "session
variables are set !"; include
'02footer_include.php';
?>
</body> </html>
03header.php
<head>
<title>PHP DEMO</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT"
crossorigin="anonymous">
</head>
02footer_include.php
<!-- To print automatically copyright year-->
<footer>
© 2012 - <?php echo date("Y"); ?>
</footer> <script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-
u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8"
crossorigin="anonymous"></script>