Yashwd
Yashwd
Practical 5
Aim : Write a PHP script to take input from an HTML form.
Name : Yash Joshi
Branch : B.tech IT
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Practical NO:5</title>
</head>
<body>
<h1>Login Form</h1>
<label for="name">Name:</label>
<label for="email">Email:</label>
</form>
Web Development 202403103520003
</body>
</html>
OUTPUT
Web Development 202403103520003
Practical 6
Aim: Write a PHP script using framework for storing and retrieving user
information from MySQL table.
a) Design a HTML registration and login page which takes name, password,email and mobile
number from user.
Name : Yash Joshi
Branch : B.tech IT
<?php
$registrationMessage = '';
$loginMessage = '';
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$mobile = htmlspecialchars($_POST['mobile']);
$password = htmlspecialchars($_POST['password']);
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
background-color: #f4f4f4;
Web Development 202403103520003
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
.container {
width: 100%;
max-width: 400px;
padding: 20px;
background: #fff;
border-radius: 8px;
h2 {
margin-top: 0;
.form-group {
margin-bottom: 15px;
.form-group label {
display: block;
margin-bottom: 5px;
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
.form-group button {
width: 100%;
padding: 10px;
background: #007bff;
border: none;
Web Development 202403103520003
color: #fff;
font-size: 16px;
cursor: pointer;
border-radius: 4px;
.form-group button:hover {
background: #0056b3;
.switch {
text-align: center;
margin-top: 15px;
.switch a {
color: #007bff;
text-decoration: none;
.switch a:hover {
text-decoration: underline;
.message {
color: green;
margin-top: 15px;
text-align: center;
</style>
</head>
<body>
<div id="registrationForm">
<h2>Register</h2>
<div class="form-group">
<label for="name">Name:</label>
</div>
<div class="form-group">
<label for="email">Email:</label>
</div>
<div class="form-group">
</div>
<div class="form-group">
<label for="password">Password:</label>
</div>
<div class="form-group">
</div>
<div class="switch">
</div>
</form>
</div>
<h2>Login</h2>
<div class="form-group">
<label for="loginEmail">Email:</label>
</div>
<div class="form-group">
<label for="loginPassword">Password:</label>
</div>
<div class="form-group">
Web Development 202403103520003
</div>
<div class="switch">
</div>
</form>
</div></div></body>
</html>
OUTPUT
Web Development 202403103520003
$stmt = $conn->prepare("INSERT INTO users (name, email, mobile, password) VALUES (?, ?, ?,
?)");
$stmt->bind_param("ssss", $name, $email, $mobile, $hashedPassword);
if ($stmt->execute()) {
$registrationMessage = "Registration successful!";
} else {
$registrationMessage = "Error: " . $stmt->error;
}
$stmt->close();
}
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
if ($stmt->num_rows > 0) {
$stmt->bind_result($hashedPassword);
$stmt->fetch();
if (password_verify($password, $hashedPassword)) {
$loginMessage = "Login successful!";
} else {
$loginMessage = "Invalid email or password.";
}
} else {
$loginMessage = "Invalid email or password.";
}
$stmt->close();
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration and Login</title>
<style>
Web Development 202403103520003
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
width: 100%;
max-width: 400px;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
margin-top: 0;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
Web Development 202403103520003
.form-group button {
width: 100%;
padding: 10px;
background: #007bff;
border: none;
color: #fff;
font-size: 16px;
cursor: pointer;
border-radius: 4px;
}
.form-group button:hover {
background: #0056b3;
}
.switch {
text-align: center;
margin-top: 15px;
}
.switch a {
color: #007bff;
text-decoration: none;
}
.switch a:hover {
text-decoration: underline;
}
.message {
color: green;
margin-top: 15px;
text-align: center;
}
</style>
</head>
<body>
Web Development 202403103520003
</html>
OUTPUT
<!DOCTYPE html>
<html lang="en">
Web Development 202403103520003
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User List</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.container {
width: 100%;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 12px;
text-align: left;
}
th {
Web Development 202403103520003
background-color: #007bff;
color: #fff;}
a{
color: #007bff;
text-decoration: none;}
a:hover {
text-decoration: underline;}
</style>
</head>
<body>
<div class="container">
<h2>Registered Users</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
// Output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["mobile"] . "</td>";
echo "</tr>";
Web Development 202403103520003
}
} else {
echo "<tr><td colspan='4'>No users found</td></tr>";
}
?>
</tbody>
</table>
<br>
<a href="display.php">Back to Home</a>
</div>
</body>
</html>
<?php
$conn->close();
?>
OUTPUT
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practical6";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update'])) {
$id = $_POST['id'];
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$mobile = htmlspecialchars($_POST['mobile']);
$password = htmlspecialchars($_POST['password']);
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("UPDATE users SET name=?, email=?, mobile=?, password=? WHERE
id=?");
$stmt->bind_param("ssssi", $name, $email, $mobile, $hashedPassword, $id);
if ($stmt->execute()) {
$updateMessage = "User updated successfully!";
} else {
$updateMessage = "Error: " . $stmt->error;
}
$stmt->close();
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete'])) {
$id = $_POST['id'];
} else {
$deleteMessage = "Error: " . $stmt->error;
}
$stmt->close();
}
$sql = "SELECT id, name, email, mobile FROM users";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User List</title>
<body>
<div class="container">
<h2>Registered Users</h2>
<div class="message"><?php if (isset($updateMessage)) echo $updateMessage; ?></div>
<div class="message"><?php if (isset($deleteMessage)) echo $deleteMessage; ?></div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
Web Development 202403103520003
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["mobile"] . "</td>";
echo "<td>";
echo "<a href='?edit=" . $row["id"] . "'>Edit</a> | ";
echo "<a href='?delete=" . $row["id"] . "' onclick='return confirm(\"Are you
sure?\")'>Delete</a>";
echo "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='5'>No users found</td></tr>";
}
?>
</tbody>
</table>
<?php if (isset($_GET['edit'])):
$editId = intval($_GET['edit']);
$stmt = $conn->prepare("SELECT id, name, email, mobile FROM users WHERE id=?");
$stmt->bind_param("i", $editId);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
?>
<h2>Update User</h2>
<form action="practical6(d).php" method="POST">
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
<div class="form-group">
Web Development 202403103520003
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo
htmlspecialchars($user['name']); ?>" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?php echo
htmlspecialchars($user['email']); ?>" required>
</div>
<div class="form-group">
<label for="mobile">Mobile Number:</label>
<input type="tel" id="mobile" name="mobile" value="<?php echo
htmlspecialchars($user['mobile']); ?>" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter new
password (leave empty to keep current)">
</div>
<div class="form-group">
<button type="submit" name="update">Update User</button>
</div>
</form>
<?php endif; ?>
<br>
<a href="practical6(d).php">Back to Home</a>
</div>
<script>
function confirmDelete() {
return confirm('Are you sure you want to delete this user?');
}
</script>
</body>
</html>
Web Development 202403103520003
<?php
$conn->close();
?>
OUTPUT
Web Development 202403103520003
Practical 7
Aim : Implement session and cookie to maintain session during login and
implement visitor counter.
Name : Yash Joshi
Branch : B.tech IT
<?php
session_start();
if (!isset($_COOKIE['visitor_count'])) {
$visitor_count = 1;
setcookie('visitor_count', $visitor_count, time() + 3600);
} else {
$visitor_count = $_COOKIE['visitor_count'] + 1;
setcookie('visitor_count', $visitor_count, time() + 3600);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = htmlspecialchars(trim($_POST['username']));
$_SESSION['username'] = $username;
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if (isset($_GET['logout'])) {
session_destroy();
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Web Development 202403103520003
OUTPUT
Web Development 202403103520003
Practical 8
Aim : Writea PHP script to create a ? ile upload and download portal.
Branch : B.tech IT
<?php
$upload_dir = 'uploads/';
if (!is_dir($upload_dir)) {
$tmp_name = $_FILES['file']['tmp_name'];
$name = basename($_FILES['file']['name']);
if (move_uploaded_file($tmp_name, $upload_file)) {
} else {
if (isset($_GET['file'])) {
$file = basename($_GET['file']);
if (file_exists($file_path)) {
header('Content-Type: application/octet-stream');
readfile($file_path);
exit();
} else {
?>
<!DOCTYPE html>
<html lang="en">
Web Development 202403103520003
<head>
<meta charset="UTF-8">
<title>Practical No :8</title>
</head>
<body>
<br>
</form>
<h2>Available Files:</h2>
<ul>
<?php
?> </ul>
</body>
</html>
OUTPUT