0% found this document useful (0 votes)
39 views26 pages

Yashwd

The document outlines practical exercises in web development using PHP, focusing on form handling and user authentication. It includes scripts for a login and registration system that interacts with a MySQL database, as well as displaying registered users in an HTML table. The document also features HTML structure and styling for user interfaces.

Uploaded by

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

Yashwd

The document outlines practical exercises in web development using PHP, focusing on form handling and user authentication. It includes scripts for a login and registration system that interacts with a MySQL database, as well as displaying registered users in an HTML table. The document also features HTML structure and styling for user interfaces.

Uploaded by

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

Web Development 202403103520003

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>

<?php if ($_SERVER["REQUEST_METHOD"] == "POST"): ?>

<h2>Form Submission Result</h2>

<p><strong>Name:</strong> <?php echo $name; ?></p>

<p><strong>Email:</strong> <?php echo $email; ?></p>

<a href="<?php echo $_SERVER['PHP_SELF']; ?>">Submit Another Response</a>

<?php else: ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<label for="name">Name:</label>

<input type="text" id="name" name="name" required><br><br>

<label for="email">Email:</label>

<input type="email" id="email" name="email" required><br><br>

<input type="submit" value="Submit">

</form>
Web Development 202403103520003

<?php endif; ?>

</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 = '';

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['register'])) {

$name = htmlspecialchars($_POST['name']);

$email = htmlspecialchars($_POST['email']);

$mobile = htmlspecialchars($_POST['mobile']);

$password = htmlspecialchars($_POST['password']);

$registrationMessage = "Registration successful! Name: $name, Email: $email, Mobile: $mobile";

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) {

$email = htmlspecialchars($_POST['email']);

$password = htmlspecialchars($_POST['password']);

$loginMessage = "Login successful! Email: $email";

?>

<!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>

body {

font-family: Arial, sans-serif;

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;

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;

.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 class="container" id="formContainer">

<div id="registrationForm">

<h2>Register</h2>

<form action="index.php" method="POST">

<div class="form-group">

<label for="name">Name:</label>

<input type="text" id="name" name="name" required>


Web Development 202403103520003

</div>

<div class="form-group">

<label for="email">Email:</label>

<input type="email" id="email" name="email" required>

</div>

<div class="form-group">

<label for="mobile">Mobile Number:</label>

<input type="tel" id="mobile" name="mobile" required>

</div>

<div class="form-group">

<label for="password">Password:</label>

<input type="password" id="password" name="password" required>

</div>

<div class="form-group">

<button type="submit" name="register">Register</button>

</div>

<div class="switch">

<p>Already have an account? <a href="#" onclick="showLogin()">Login here</a></p>

</div>

</form>

<div class="message"><?php echo $registrationMessage; ?></div>

</div>

<div id="loginForm" style="display: none;">

<h2>Login</h2>

<form action="index.php" method="POST">

<div class="form-group">

<label for="loginEmail">Email:</label>

<input type="email" id="loginEmail" name="email" required>

</div>

<div class="form-group">

<label for="loginPassword">Password:</label>

<input type="password" id="loginPassword" name="password" required>

</div>

<div class="form-group">
Web Development 202403103520003

<button type="submit" name="login">Login</button>

</div>

<div class="switch">

<p>Don't have an account? <a href="#" onclick="showRegister()">Register here</a></p>

</div>

</form>

<div class="message"><?php echo $loginMessage; ?></div>

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

</html>

OUTPUT
Web Development 202403103520003

b. Store this data in MySQLdatabase


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practical6";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$registrationMessage = '';
$loginMessage = '';

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['register'])) {


$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("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();
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) {


Web Development 202403103520003

$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);

$stmt = $conn->prepare("SELECT password FROM users WHERE email = ?");


$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();

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

<div class="container" id="formContainer">


<!-- Registration Form -->
<div id="registrationForm">
<h2>Register</h2>
<form action="practical6(B).php" method="POST">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="mobile">Mobile Number:</label>
<input type="tel" id="mobile" name="mobile" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<button type="submit" name="register">Register</button>
</div>
<div class="switch">
<p>Already have an account? <a href="#" onclick="showLogin()">Login here</a></p>
</div>
</form>
<div class="message"><?php echo $registrationMessage; ?></div>
</div>

<!-- Login Form -->


Web Development 202403103520003

<div id="loginForm" style="display: none;">


<h2>Login</h2>
<form action="index.php" method="POST">
<div class="form-group">
<label for="loginEmail">Email:</label>
<input type="email" id="loginEmail" name="email" required>
</div>
<div class="form-group">
<label for="loginPassword">Password:</label>
<input type="password" id="loginPassword" name="password" required>
</div>
<div class="form-group">
<button type="submit" name="login">Login</button>
</div>
<div class="switch">
<p>Don't have an account? <a href="#" onclick="showRegister()">Register
here</a></p>
</div>
</form>
<div class="message"><?php echo $loginMessage; ?></div>
</div>
</div>
<script>
function showLogin() {
document.getElementById('registrationForm').style.display = 'none';
document.getElementById('loginForm').style.display = 'block';
}
function showRegister() {
document.getElementById('loginForm').style.display = 'none';
document.getElementById('registrationForm').style.display = 'block';
}
</script>
</body>
Web Development 202403103520003

</html>

OUTPUT

b) On next page display all user in HTML table using PHP.


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practical6";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, email, mobile FROM users";
$result = $conn->query($sql);
?>

<!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

c) Update/Delete details of user.


<?php
Web Development 202403103520003

$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'];

$stmt = $conn->prepare("DELETE FROM users WHERE id=?");


$stmt->bind_param("i", $id);
if ($stmt->execute()) {
$deleteMessage = "User deleted successfully!";
Web Development 202403103520003

} 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

<title>Visitor Counter and Login</title>


</head>
<body>
<h1>Visitor Counter and Login</h1>
<?php if (isset($_SESSION['username'])): ?>
<h2>Hello, <?php echo $_SESSION['username']; ?>!</h2>
<p>You are visitor number <?php echo $visitor_count; ?>.</p>
<a href="?logout=true">Logout</a>
<?php else: ?>
<h2>Login</h2>
<form method="post" action="">
<input type="text" name="username" placeholder="Enter your username" required>
<button type="submit">Login</button>
</form>
<p>You are visitor number <?php echo $visitor_count; ?>.</p> <?php endif; ?></body>
</html>

OUTPUT
Web Development 202403103520003

Practical 8
Aim : Writea PHP script to create a ? ile upload and download portal.

Name : Yash Joshi

Branch : B.tech IT

<?php

$upload_dir = 'uploads/';

if (!is_dir($upload_dir)) {

mkdir($upload_dir, 0777, true);

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {

$tmp_name = $_FILES['file']['tmp_name'];

$name = basename($_FILES['file']['name']);

$upload_file = $upload_dir . $name;

if (move_uploaded_file($tmp_name, $upload_file)) {

$message = 'File uploaded successfully.';

} else {

$message = 'Failed to upload file.';}}

if (isset($_GET['file'])) {

$file = basename($_GET['file']);

$file_path = $upload_dir . $file;

if (file_exists($file_path)) {

header('Content-Description: File Transfer');

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');

header('Content-Length: ' . filesize($file_path));

readfile($file_path);

exit();

} else {

$message = 'File not found.';} }

?>

<!DOCTYPE html>

<html lang="en">
Web Development 202403103520003

<head>

<meta charset="UTF-8">

<title>Practical No :8</title>

</head>

<body>

<h1>File Upload and Download Portal</h1>

<form action="" method="post" enctype="multipart/form-data">

<label for="file">Select file to upload:</label>

<input type="file" id="file" name="file" required>

<br>

<input type="submit" value="Upload">

</form>

<?php if (isset($message)): ?>

<p><?php echo htmlspecialchars($message); ?></p>

<?php endif; ?>

<h2>Available Files:</h2>

<ul>

<?php

$files = array_diff(scandir($upload_dir), array('.', '..'));

foreach ($files as $file) {

echo '<li><a href="?file=' . urlencode($file) . '">' . htmlspecialchars($file) . '</a></li>'; }

?> </ul>

</body>

</html>

OUTPUT

You might also like