php practical FINAL
php practical FINAL
PROGRAM 1
*/
Solution:
<?php
$pattern = "/fo[rl]ks\?+/";
$text = "Could you please pass the forks? , the folks at the dinner table politely requested.";
?>
In this example, the regular expression fo[rl]ks\?+ matches the string “folks” in the text. The [rl] is a
character class that matches either "r" or "l", and the \? is a metacharacter that matches the "?"
character. The + is a quantifier that matches one or more of the preceding character, so \?+ matches one
or more "?" characters. The i modifier is not used in this example, but it can be added to the end of the
regular expression to make the search case-insensitive.
PROGRAM 2
*/
Solution :
Nested_if.html
<html>
<head>
<title> age verification </title>
</head>
<body>
<h2>Check Age</h2>
</body>
</html>
Nested_if.php
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$age = $_POST["age"];
if (is_numeric($age))
{
if ($age < 18)
{
echo "You are " . $age . " years old, so you are not an adult yet.";
}
else if ($age >= 18 && $age <= 60)
{
echo "You are " . $age . " years old, so you are an adult.";
}
else if ( $age > 60 && $age <= 100 )
{
echo "You are " . $age . " years old, so you are a sinior citizon .";
}
else
{
echo "Invalid age entered. Please enter a valid age";
}
}
else
{
echo "Invalid age entered. Please enter a valid age";
}
}
?>
</body>
</html>
OUTPUT :
PROGRAM 3
/*Program 3 : : Write a program to create a menu driven program and show the
usage of switch-case.?
Name : SAKSHAM GUPTA
Roll No 03714202023
*/
SOLUTION :
SWITCH.HTML
<html>
<head>
<title>calculator</title>
</head>
<body>
<h1>Calculator</h1>
SWITCH.PHP
<html>
<head>
<title>Calculator</title>
</head>
<body>
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];
switch ($operator)
{ case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 == 0) {
echo "Error: Division by zero is not allowed.";
exit();
}
$result = $num1 / $num2;
break;
default:
echo "Error: choose an option";
exit();
}
</html>
OUTPUT:
PROGRAM 4
/*Program 4: Write a program to show the usage of for/while/do while loop ?
Name : SAKSHAM GUPTA
Roll No 03714202023
*/
SOLUTION:
i) FOR LOOP –
<html>
<head>
<title>Print Numbers</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Print Numbers</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$number = $_POST["number"];
OUTPUT :
<html>
<head>
<title>Print Numbers</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Print Numbers</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
$i = 1;
OUTPUT :
<html>
<head>
<title>Print Numbers</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Print Numbers</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
$i = 1;
</body>
</html>
OUTPUT :
PROGRAM 5
*/
SOLUTION
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sorting Algorithms</title>
</head>
<body bgcolor="#01B4FF">
<h1>Sorting Algorithms</h1>
<form method="post">
<label for="numbers">Enter numbers (comma-separated):</label>
<input type="text" id="numbers" name="numbers" required>
<input type="submit" value="Sort">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = $_POST['numbers'];
$numbers = explode(',', $input);
printArray($numbers);
// Bubble Sort
$bubbleSorted = bubbleSort($numbers);
echo "<h2>Bubble Sort:</h2>";
printArray($bubbleSorted);
// Selection Sort
$selectionSorted = selectionSort($numbers);
echo "<h2>Selection Sort:</h2>";
printArray($selectionSorted);
// Insertion Sort
$insertionSorted = insertionSort($numbers);
echo "<h2>Insertion Sort:</h2>";
printArray($insertionSorted);
// Quick Sort
$quickSorted = quickSort($numbers);
echo "<h2>Quick Sort:</h2>";
printArray($quickSorted);
}
function bubbleSort($arr) {
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++)
{ if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}
function selectionSort($arr) {
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
$minIndex = $i;
for ($j = $i + 1; $j < $n; $j++) {
if ($arr[$j] < $arr[$minIndex]) {
$minIndex = $j;
}
}
$temp = $arr[$i];
$arr[$i] = $arr[$minIndex];
$arr[$minIndex] = $temp;
}
return $arr;
}
function insertionSort($arr) {
$n = count($arr);
for ($i = 1; $i < $n; $i++) {
$key = $arr[$i];
$j = $i - 1;
while ($j >= 0 && $arr[$j] > $key) {
$arr[$j + 1] = $arr[$j];
$j = $j - 1;
}
$arr[$j + 1] = $key;
}
return $arr;
}
function quickSort($arr) {
$n = count($arr);
if ($n <= 1) {
return $arr;
}
$pivot = $arr[0];
$left = $right = [];
function printArray($arr)
{ echo "<pre>";
print_r($arr);
echo "</pre>";
}
?>
</body>
</html>
OUTPUT :
PROGRAM 6
/* Program 6:- Write al Program to implement array, pad(),array_ slice(),array.
Splice(),list()
Name : SAKSHAM GUPTA
Roll No: 03714202023
*/
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Array Functions</title>
<link rel="stylesheet" href="array_functions.css">
</head>
<body>
<hl>Array Functions</h1>
<div class="container">
<div class="output">
<h2>Array Pad</h2>
<?php
$original_array =[1, 2, 3];
$padded_array = array_pad($original_array, 5, 0);
echo "<p>Original Array: ".implode(", ", $original_array)."</p>";
echo "<p>Array Pad:". implode(", ", $padded_array)."</p>";
?>
</div>
<div class="output">
<h2>Array Slice</h2>
<?php
$sliced_array = array_slice($original_array, 1, 2);
echo "<p>Original Array: ".implode(", ", $original_array)."</p>";
echo "<p>Array Slice: ". implode(", ", $sliced_array)."</p>"; ?>
</div>
<div class="output">
<h2>Aray Splice</h2>
<?php
$spliced_array = $original_array;
array_splice($spliced_array, 1,2,[4,5]);
echo "<p>Original Array: " .implode(",",$original_array). "</p>";
echo "<p>Array Splice: ".implode(", ", $spliced_array). "/p>"; ?>
</div>
<div class="output">
<h2>List</h2>
<?php
list($a, $b, $c)= $original_array;
echo "<p>Original Array: ".implode(", ", $original_array)."</p>";
echo "<p>List: a =$a,b= $b, c= $c</p>"; ?>
</div></div>
</body>
</html>\
Css
body {
font-family: Arial, sans-serif;
padding: 10px;
background-color: aquamarine;
}
hl {
text-align: center;
}
.container
{ display:
center;
justify-content: center;
margin-top: 10px;
}
.output {
border: 5px solid #1a1a1a;
padding: 10px;
margin: 10px 10px;
background-color:
azure;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 20
Practical – I [WEB BASED PGM Lab] [BCA-104]
OUTPUT :
PROGRAM 7
/* Program 7:- Write a program to implement functions.(use foreach wherever
applicable)
Name : SAKSHAM GUPTA
Roll No: 03714202023
*/
SOLUTION
<!DOCTYPE html>
<html>
<head>
<title>PHP Functions</title>
<link rel="stylesheet" href="foreach.css">
</head>
<body>
<hl>PHP Functions</h1>
<div class="container">
<div class="output">
<h2>Factorial Calculation</h2>
<?php
function factorial($n) {
if($n==0){
return 1;
}
else
{
return $n * factorial($n - 1);}
}
$number =5;
echo "Factorial of $number is".factorial($number);
?>
<div>
<div class="output">
<h2>Reverse of Array</h2>
<?php
function reverse_array($arr)
{ return array_reverse($arr);
}
$original_array=[1, 2, 3,4,5];
$reversed_array= reverse_array($original_array);
foreach ($reversed_array as $element)
{
echo $element.",";
}
?>
</div>
</div>
</body>
</html>
CSS
body {
font-family: Arial, sans-serif;
padding: 10px;
background-color: aquamarine;
}
hl {
text-align: center;
}
.container
{ display:
center;
justify-content: center;
margin-top: 10px;
}
.output {
border: 5px solid #1a1a1a;
padding: 10px;
margin: 10px 10px;
background-color:
azure;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 23
Practical – I [WEB BASED PGM Lab] [BCA-104]
OUTPUT :
PROGRAM 8
/* Program 8:- Write a program that passes control to another page (include , require ,
exit and die functions )
Name : SAKSHAM GUPTA
Roll No : 03714202023
*/
SOLUTION
<!DOCTYPE html>
<html>
<head>
<title>Control Transfer</title>
<link rel="stylesheet" href="Pass_control.css">
</head>
<body>
<hl>Control Transfer</h1>
<div class="container">
<div class="output">
<h2>Include</h2>
<?php
echo "Before include statement<br><br>";
$included_text = "Included text";
echo $included_text;
echo "<br><br>After include statement";?>
</div>
<div class="output">
<h2>Require</h2>
<?php
echo "Before require statement<br><br>";
$required_text = "This is required text";
echo $required_text;
echo "<br><br>After require statement";?>
</div>
<div class="output">
<h2>Exit</h2>
<?php
CSS
body {
font-family: Arial, sans-serif;
padding: 30px;
background-color: yellow;
}
hl {
text-align: center;
}
.container{ dis
play: flex;
justify-content: center;
margin-top: 20px;
}
.output{
border: 2px solid #092531;
padding: 10px;
margin: 10px 10px;
background-color: cornflowerblue;
}
OUTPUT :
PROGRAM 9
/* Program 9 :- Write a program to show the usage of Cookie?
Name : SAKSHAM GUPTA
Roll No : 03714202023
*/
SOLUTION
<!DOCTYPE html>
<html>
<head>
<title> Cookie Example</title>
<link rel="stylesheet" href="Cookies.css">
</head>
<body>
<hl>Consent Form</h1>
<div class="container">
<div class="output">
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone'])) {
setcookie('username', $_POST['name'], time() + 3600); // Set cookie for 1 hour
setcookie('useremail', $_POST['email'], time() + 3600); // Set cookie for 1 hour
setcookie('userphone', S_POST['phone'], time() + 3600); // Set cookie for 1 hour
echo "Hello, " .$_POST['name']. "! Your information has been stored.";
}
elseif (isset($_COOKIE['username']) && isset($_COOKIE['useremail']) &&
isset($_COOKTE['userphone'])) {
echo "Hello, ". $_COOKIE['username']. "!Welcome back.";
echo "<br>Your email: ".$_COOKIE['useremail'];
echo "<br>Your phone number: ". $_COOKIE['userphone'];
}?>
</div> </div>
<div class="container">
<form method="post" action="">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Enter your email: </label>
CSS
body {
font-family: Arial, sans-serif;
padding: 10px;
background-color: aquamarine;
}
hl {
text-align: center;
}
.container
{ display:
center;
justify-content: center;
margin-top: 10px;
}
.output {
border: 5px solid #1a1a1a;
padding: 10px;
margin: 10px 10px;
background-color:
azure;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 29
Practical – I [WEB BASED PGM Lab] [BCA-104]
OUTPUT :
PROGRAM 10
/* Program 10 :- Write a program to show the usage of session ?
Name : SAKSHAM GUPTA
Roll No 03714202023
*/
SOLUTION
<?php
session_start();
$valid_username = 'user';
$valid_password = 'password';
if (isset($_POST['login']))
$username=$_POST['username'];
$password=$_POST['password'];
if ($username === $valid_username && $password === $valid_password) {
$_SESSION['loggedin']=true;
$_SESSION['username'] = $username;
header('Location: protected.php');
exit;
} else {
$error = 'Invalid username or password.';
}
if (isset($_POST['logout'])) {
session_unset();
session_destroy();
}?>
<!DOCTYPE html>
<html>
<head>
<title>Session Example - User Authentication</title>
<link rel="stylesheet" href="session.css">
</head>
<body>
<hl>User Authentication</h1>
<div class="container">
<div class="output">
<?php
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: pink;}
h1 {text-align: center;}
.container{ d
isplay: flex;
justify-content: center;
margin-top: 20px;}
.output {
border: 1px solid
#ccc; padding: 10px;
margin: 0 10px;
}
form {text-align: center;}
OUTPUT :
PROGRAM 11
/* Program 11 :- Write a program to implement oops concept.
Name : SAKSHAM GUPTA
Roll No 03714202023
*/
Q11.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple OOP Example</title>
<link rel="stylesheet" href="styl.css">
</head>
<body>
<h1>Simple OOP Example</h1>
<form action="process.php" method="post">
<label for="color">Enter Car Color:</label>
<input type="text" name="color" id="color" required><br>
<label for="model">Enter Car Model:</label>
<input type="text" name="model" id="model" required><br>
<label for="year">Enter Car Year:</label>
<input type="number" name="year" id="year" min="1900" max="2022" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
PROCESS.PHP
<?php
require_once 'Car.php';
$color = $_POST['color'];
$model = $_POST['model'];
$year = $_POST['year'];
$myCar = new Car($color, $model, $year);
echo "The car is a ". $myCar->color. " ". $myCar->model. " from ". $myCar->year. ".";
?>
CAR.PHP
<?php
class Car {
public $color;
public $model;
public $year;
?>
SLYL.CSS
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
form {
margin-top: 20px;
}
label {
display: block;
margin-top: 10px;
}
input[type="text"], input[type="number"]
{ width: 100%;
padding: 5px;
margin-top: 5px;
}
button {
margin-top: 20px;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
OUTPUT:
PROGRAM 12
Q12.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Personal Information Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Personal Information Form</h2>
<form action="process_form.php" method="post">
Style.css
body {
font-family: 'Roboto', sans-serif;
background-color: #f09191;}
h2 {
text-align: center;
color: #333;}
form {
width: 300px;
background-color: #fff;
padding: 30px;
margin: 0 auto;
margin-top: 50px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.1);
border-radius: 5px;}
label {
display: block;
margin-top: 10px;
color: #333;}
input[type="submit"]
{ background-color:
#4CAF50; color: white;
padding: 14px 20px;
border: none;}
Process_form.php
<?php
session_start();
$_SESSION['uname'] = $_POST['uname'];
$_SESSION['pword'] = $_POST['pword'];
header('Location: login_form.html');
exit;
?>
Login.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Login Form</h2>
<form action="process_login.php" method="post">
<label for="uname">Username:</label>
<input type="text" id="uname" name="uname"><br><br>
<label for="pword">Password:</label>
<input type="password" id="pword" name="pword"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Process_login.php
<?php
session_start();
$username = $_SESSION['uname'];
$password = $_SESSION['pword'];
Logout.php
<?php
session_start();
session_destroy();
echo "You have been logged out.";
echo "<form action='q12.html' method='post'>";
echo "<br><br><button type='submit'>go to personal information form</button>";
echo "</form>";
exit;
?>
Output:
PROGRAM 13
<!DOCTYPE html>
<html lang="en">
<head>
<title>a program that use various PHP library functions, and that manipulate files and
directories.</title>
</head>
<body>
<?php
if (file_exists('example.txt'))
{
$file = fopen('example.txt', 'r+');
$contents = file_get_contents('example.txt');
echo '<h4>before writing in file</h4><br>';
while (!feof($file))
{
$line = fgets($file);
echo '<br>Line: '. $line;
}
while (!feof($file))
{
$line = fgets($file);
echo '<br>Line: '. $line;
}
fclose($file);
}
else
{
mkdir('example_dir');
if (is_dir('example_dir'))
{
echo 'The directory was created.';
}
}
?>
</body>
</html>
OUTPUT :
PROGRAM 14
<?php
$file = 'q14.txt';
$currentContent = file_get_contents($file);
$newContent = $currentContent. "\n". "this is php coding. it is used to creat webpages and it
is used with html.\n";
file_put_contents($file, $newContent);
OUTPUT :
PROGRAM 15
Index.php
<!DOCTYPE html>
<html>
<head>
<style>
.form-container {
width: 100%;
max-width: 500px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f2f2f2;
text-align: center;
}
.form-container input[type="file"]
{ width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
.form-container input[type="submit"] {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: #fff;
cursor: pointer;
margin-top: 10px;
}
</style>
</head>
<body>
<div >
<img src="uploads/<?php echo @htmlspecialchars(basename($_FILES["fileToUpload"]
["name"])); ?>" >
</div>
</body>
</html>
Upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if (isset($_POST["submit"]))
{
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false)
{
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else
{
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file))
{
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . "
has been uploaded. <br><br>";
}
else
{
echo "Sorry, there was an error uploading your file. <br><br>";
}
}
OUTPUT:
PROGRAM 16
/* Program 16 :- Use phpMyAdmin and perform the following : import, review data
and structure, run SQL statements, create users and privileges.
IMPORT :
PROGRAM 17
<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
else
{
echo 'connection created successfully <br><br>';
}
$conn->close();
?>
OUTPUT:
PROGRAM 18
/* Program 18 : Write a program to select all the records and display it in table. CO1,
CO2
Name : SAKSHAM GUPTA
Roll No 03714202023
*/
<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
$dbname = "student";
if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
else
{
echo "DATABASE Connected successfully<br><br>";
}
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>". $row["Sno."]. "</td>";
echo "<td>". $row["F_NAME"]. "</td>";
echo "<td>". $row["L_NAME"]. "</td>";
echo "<td>". $row["COURCE"]. "</td>";
echo "</tr>";
echo "</table>";
$conn->close();
?>
OUTPUT:
PROGRAM 19
UPDATE.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>sql alter table</title>
<style>
form {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid
#ccc; border-radius:
5px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"],
input[type="number"],
select {
width: 100%;
padding: 5px;
margin-bottom: 10px;
border: 1px solid
#ccc; border-radius:
3px;
}
SAKSHAM GUPTA [Roll No: 03714202023] pg. 56
Practical – I [WEB BASED PGM Lab] [BCA-104]
input[type="submit"]
{ background-color:
#4CAF50; color: white;
padding: 10px 20px;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<form action="update.php" method="post">
<label for="action">Action:</label>
<select name="action">
<option value="add">Add</option>
<option value="modify">Modify</option>
<option value="delete">Delete</option>
</select>
<br>
<label for="id">sno.</label>
<input type="number" name="sno." id="sno.">
<br>
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname">
<br>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname">
<br>
<label for="course">Course:</label>
<input type="text" name="course" id="course">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
UPDATE.PHP
<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
$dbname = "student";
if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
switch ($_POST['action'])
{
case 'delete':
$Sno = $_POST['Sno.'];
$sql = "DELETE FROM student_data WHERE Sno. = $Sno";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record: ". $conn->error;
}
break;
case 'modify':
$Sno = $_POST['Sno.'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$course = $_POST['course'];
$sql = "UPDATE student_data
SET F_NAME = '$fname', L_NAME = '$lname', COURCE = '$course'
WHERE Sno = $Sno";
if ($conn->query($sql) === TRUE)
{
echo "Record updated successfully";
}
else
{
echo "Error updating record: ". $conn->error;
}
break;
case 'add':
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$course = $_POST['course'];
$sql = "INSERT INTO student_data (F_NAME, L_NAME, COURCE)
VALUES ('$fname', '$lname', '$course')";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
} else
{
echo "Error: ". $sql. "<br>". $conn->error;
}
break;
default:
OUTPUT :
PROGRAM 20
REGISTER.HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h2>REGISTRATION FORM</h2>
<div class="container">
<h2>Register now!</h2>
<form action="process_form.php" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" required><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br>
<button type="submit">Register</button>
</form>
</div>
</body>
</html>
STYLS.CSS
body
{
background-color: lightgray;
}
.container {
width: 400px;
padding: 16px;
background-color: white;
margin: 0 auto;
margin-top: 50px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.container h2
{ text-align:
center;
margin-bottom: 20px;
}
.container label
{ display: block;
margin-bottom: 5px;
}
.container button[type="submit"]
{ width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
}
h2 {
font-size: 24px;
color: #333;
margin-bottom: 20px;
text-align: center;
}
button[type="submit"] {
width: 50%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
display: block;
margin: 0 auto;
p{
text-align: center;
}
PROCESS_FORM.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>registration</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
?>
<?php
$servername = "localhost";
$username = "SAKSHAM";
$password = "123";
$dbname = "USER_DETAILS";
if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$stmt->execute();
$stmt->close();
$conn->close();
?>
</body>
</html>
LOGIN_FORM.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Form</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
</head>
<body>
<h2>LOGIN PAGE</h2>
<div class="container">
<h2>Enter your details</h2>
<form action="process_login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="pword">Password:</label>
<input type="password" id="pword" name="password"><br><br>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
PROCESS_LOGIN.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>LOGGING IN</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
session_start();
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
$validLogin = false;
if ($validLogin) {
echo "<p>Login successful!</p>";
echo "<br><br><form action='logout.php' method='post'>";
echo "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati vero labore
cupiditate quibusdam et, nihil earum nobis, similique officiis incidunt veniam quaerat saepe
maiores laboriosam, voluptatibus harum itaque sit? Possimus. Lorem ipsum dolor sit amet
consectetur adipisicing elit. Quia modi alias amet enim blanditiis consectetur reprehenderit eos
repellendus corporis at. Architecto aspernatur voluptas explicabo aliquam dicta accusamus
eius, maiores quaerat! Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatibus,
magni, perferendis labore sequi possimus repudiandae ea fugit eveniet sunt expedita aut quod
numquam accusamus nostrum illum. Qui ea quaerat eos.<br><br>";
echo "<button type='submit'>Logout</button>";
echo "</form>";
} else {
echo "<p>Invalid username or password.</p>";
echo "<br><br><form action='login_form.html' method='post'>";
echo "<button type='submit'>Try again!</button>";
echo "</form>";
}
?>
</body>
</html>
LOGOUT.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>LOGGED OUT</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
session_start();
session_destroy();
echo "<p>You have been logged out.</p>";
echo "<form action='register.html' method='post'>";
echo "<br><br><button type='submit'>go to registration form</button>";
echo "</form>";
echo "<form action='login_form.HTML' method='post'>";
echo "<br><br><button type='submit'>go to login form</button>";
echo "</form>";
exit;
?>
</body>
</html>
OUTPUT: