0% found this document useful (0 votes)
124 views

PHP 29 1

Uploaded by

feyage1646
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)
124 views

PHP 29 1

Uploaded by

feyage1646
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/ 41

Krish Nandasana TY-BCA - C ROLL NO.

- 29

Q1 Create a simple HTML form and accept the user name and display the name
through PHP echo statement.

Code =>
<!DOCTYPE html>

<html>

<head>

< tle>Name Display Form</ tle>

</head>

<body>

<h2>Enter Your Name</h2>

<form method="POST" ac on="">

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

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

<button type="submit">Submit</button>

</form>

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

$name = $_POST["name"]; echo "<h2>Hello, $name</h2>";

?>

</body>

</html>

WEB FRAMEWORK AND SERVICES 1


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q2 Write a program to check whether the givennumber is prime or not.

Code :

<?php

function isPrime($number) {

if ($number <= 1) {

return false;

if ($number <= 3) {

return true;

if ($number % 2 == 0 || $number % 3 == 0) {

return false;

for ($i = 5; $i * $i <= $number; $i += 6) {

if ($number % $i == 0 || $number % ($i + 2) == 0) {

return false;

return true;

$number = 64;

if (isPrime($number)) {

echo "$number is a prime number.";

} else {

echo "$number is not a prime number.";

?>

WEB FRAMEWORK AND SERVICES 2


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q3 Write a program to check given number is odd or even.

Code =

<?php

$number = 48;

if ($number % 2 == 0) {

echo "$number is even.";

} else {

echo "$number is odd.";

?>

WEB FRAMEWORK AND SERVICES 3


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q4 Write a program to find the greatest number in three given numbers.

Code =

<?php

$num1 = 41;

$num2 = 18;

$num3 = 48;

if ($num1 >= $num2 && $num1 >= $num3) {

echo "$num1 is the greatest number.";

} elseif ($num2 >= $num1 && $num2 >= $num3) {

echo "$num2 is the greatest number.";

} else {

echo "$num3 is the greatest number.";

?>

WEB FRAMEWORK AND SERVICES 4


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q5 Print the numbers 1 - 10 in reverse order.

Code =

<?php

for ($i = 10; $i >= 1; $i--) {

echo "$i ";

?>

WEB FRAMEWORK AND SERVICES 5


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q6 Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one line. There will be no hyphen(-
)at starƟng and ending posiƟon

Code =

<?php

for ($i = 1; $i <= 10; $i++) {

echo $i;

if ($i != 10) {

echo "-";

?>

WEB FRAMEWORK AND SERVICES 6


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q7 Write a PHP program to generate and display the first n lines of a Floyd triangle.
Sample output for n = 5 :12 34 5 67 8 9 1011 12 13 14 15

Code =

<?php

$n = 5;

$num = 1;

for ($row = 1; $row <= $n; $row++) {

for ($col = 1; $col <= $row; $col++) {

echo $num . " ";

$num++;
}

echo "<br>";

?>

WEB FRAMEWORK AND SERVICES 7


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q8 .Write a PHP program to generate and display the first n lines of a Floyd triangle.
Sample output for n = 5 :12 34 5 67 8 9 1011 12 13 14 15

Code =

<?php

$userNumber = 7;

if ($userNumber < 1 || $userNumber > 10) {

echo "Please enter a number between 1 and 10.";

} else {

for ($i = 1; $i <= 10; $i++) {

if ($i == $userNumber) {

continue;

}
echo $i . " ";

?>

WEB FRAMEWORK AND SERVICES 8


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q9. Write a PHP program to generate and display the first n lines of a Floyd triangle.
Sample output for n = 5 :12 34 5 67 8 9 1011 12 13 14 15

Code =

<?php

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

$value1 = $_POST["value1"];

$value2 = $_POST["value2"];

$action = $_POST["action"];

$result = 0;

switch ($action) {

case "add":

$result = $value1 + $value2;

break;

case "subtract":

$result = $value1 - $value2;

break;

case "multiply":

$result = $value1 * $value2;

break;

case "divide":

if ($value2 != 0) {

$result = $value1 / $value2;

} else {

$result = "Cannot divide by zero";

break;

?>

WEB FRAMEWORK AND SERVICES 9


Krish Nandasana TY-BCA - C ROLL NO. - 29

<!DOCTYPE html>

<html>

<head>

<title>ArithmeƟc Calculator</title>

</head>

<body>

<h2>Arithmetic Calculator</h2>

<form method="POST" action="">

<label for="value1">Value 1:</label>

<input type="number" name="value1" id="value1" required><br><br>

<label for="value2">Value 2:</label>

<input type="number" name="value2" id="value2" required><br><br>

<label for="action">Select an operation:</label>

<select name="action" id="action">

<option value="add">Add</option>

<option value="subtract">Subtract</option>

<option value="multiply">Multiply</option>

<option value="divide">Divide</option>

</select><br><br>

<button type="submit">Calculate</button>

</form>

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

<h3>Result:</h3>

<p><?php echo $result; ?></p>

<?php endif; ?>

</body>

</html>

WEB FRAMEWORK AND SERVICES 10


Krish Nandasana TY-BCA - C ROLL NO. - 29

WEB FRAMEWORK AND SERVICES 11


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q10. Write a program to print the mulƟplicaƟon table of the number entered by the

user

Code =

<?php

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

$number = $_POST["number"];

?>

<!DOCTYPE html>

<html>

<head>

<Ɵtle>MulƟplicaƟon Table</Ɵtle>

</head>

<body>

<h2>MulƟplication Table</h2>

<form method="POST" acƟon="">

<label for="number">Enter a number:</label>

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

<buƩon type="submit">Generate Table</buƩon>

</form>

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

<h3>Multiplication Table for <?php echo $number; ?>:</h3>

<table border="1">

<tr>

<th>Multiplier</th>

<th>Result</th>

</tr>

<?php

for ($i = 1; $i <= 10; $i++) {

WEB FRAMEWORK AND SERVICES 12


Krish Nandasana TY-BCA - C ROLL NO. - 29

$result = $number * $i;

echo "<tr>";

echo "<td>$i</td>";

echo "<td>$result</td>";

echo "</tr>";

?>

</table>

<?php endif; ?>

</body>

</html>

WEB FRAMEWORK AND SERVICES 13


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q11. Write a program to check whether the given number is Armstrong or not.

Code =

<?php

function isArmstrong($num) {

$originalNum = $num;

$sum = 0;

$numDigits = strlen($num);

while ($num > 0) {

$digit = $num % 10;

$sum += pow($digit, $numDigits);

$num = (int)($num / 10);

return $originalNum == $sum;

$number = 153;

if (isArmstrong($number)) {

echo "$number is an Armstrong number.";

} else

echo "$number is not an Armstrong number.";

?>

WEB FRAMEWORK AND SERVICES 14


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q12. Write a Program to display fruit names & that values within a table

Code =

<!DOCTYPE html>

<html>

<head>

<title>Fruit Table</title>

</head>

<body>

<h2>Fruit Table</h2>

<table border="1">

<tr>

<th>Fruit Name</th>

<th>Value</th>

</tr>

<?php

$fruits = array(

"Apple" => 1,

"Banana" => 2,

"Orange" => 3,

"Watermelon" => 5,

"Mango" => 6,

"Pineapple" => 7,

"Strawberry" => 8,

"Peach" => 10

);

foreach ($fruits as $fruit => $value) {

echo "<tr>";

echo "<td>$fruit</td>";

echo "<td>$value</td>";

WEB FRAMEWORK AND SERVICES 15


Krish Nandasana TY-BCA - C ROLL NO. - 29

echo "</tr>";

?>

</table>

</body>

</html>

WEB FRAMEWORK AND SERVICES 16


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q13. Write a program to find out the sum of the array

Code =

<?php

$numbers = array(64,74);

$sum = 0;

foreach ($numbers as $number) {

$sum += $number;

echo "Sum of the array elements: $sum";

?>

WEB FRAMEWORK AND SERVICES 17


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q14 .Write a program to find the greatest and the smallest element of the given array.

Code =

<?php

$numbers = array(1, 45, 4, 61, 21, 10);

$min = $numbers[0];

$max = $numbers[0];

foreach ($numbers as $number) {

if ($number < $min) {

$min = $number;

if ($number > $max) {

$max = $number;

echo "Smallest element in the array: $min<br>";

echo "Greatest element in the array: $max";

?>

WEB FRAMEWORK AND SERVICES 18


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q15. Write a program to find the greatest and the smallest element of the given array.

Code =

<?php

$matrix = array(

array(1, 2, 3),

array(4, 5, 6),

array(7, 8, 9)

);

$userNumber = 3;

echo "Original Matrix:<br>";

for ($i = 0; $i < 3; $i++) {

for ($j = 0; $j < 3; $j++) {

echo $matrix[$i][$j] . " ";

}
echo "<br>";

echo "<br>MulƟplying the matrix by $userNumber:<br>";

for ($i = 0; $i < 3; $i++) {

for ($j = 0; $j < 3; $j++) {

echo ($matrix[$i][$j] * $userNumber) . " ";

echo "<br>";

?>

WEB FRAMEWORK AND SERVICES 19


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q16. Writeaprogramtocheckwhetherthegivenstringisa palindromeornot.(Eg:Malayalam)

Code =

<?php

function isPalindrome($string) {

$string = strtolower($string);

$string = str_replace(' ', '', $string);

$reversedString = strrev($string);

return $string === $reversedString;

$testString = "drashti";

if (isPalindrome($testString)) {

echo "$testString is a palindrome.";

} else {

echo "$testString is not a palindrome.";

?>

WEB FRAMEWORK AND SERVICES 20


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q17. WritetheprogramtogeƩheregistraƟonpageinformaƟonin
htmlandshowthaƟnformaƟonusingPHP

Code =

17-1.php =

<!DOCTYPE html>

<html>

<head>

<title>Registration Page</title>

</head>

<body>

<h2>Registration Page</h2>

<form method="POST" action="17-2.php">

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

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

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

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

<label for="username">Username:</label>

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

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

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

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

</form>

</body>

</html>

17-2.php =

WEB FRAMEWORK AND SERVICES 21


Krish Nandasana TY-BCA - C ROLL NO. - 29

<!DOCTYPE html>

<html>

<head>

<title>User Information</title>

</head>

<body>

<h2>User Information</h2>

<?php

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

$name = $_POST["name"];

$email = $_POST["email"];

$username = $_POST["username"];

$password = $_POST["password"];

echo "<p><strong>Name:</strong> $name</p>";

echo "<p><strong>Email:</strong> $email</p>";

echo "<p><strong>Username:</strong> $username</p>";

echo "<p><strong>Password:</strong> $password</p>";

}
?>

</body>

</html>

WEB FRAMEWORK AND SERVICES 22


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q18. Writeaprogramtoge &


heusername&passwordfromuser.Ifhegivenusernameis“admin” and
passwordis“godsgift”showthewelcomemessage otherwiseshowtheerrorandredirect to
login page.

Code =

File 1

<!DOCTYPE html>

<html>

<head>

<title>Login Page</title>

</head>

<body>

<h2>Login Page</h2>

<form method="POST" action="18-2.php.">

<label for="username">Username:</label>

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

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

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

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

</form>

</body>

</html>

File 2

<!DOCTYPE html>

<html>

<head>

<title>Login Status</title>

</head>

WEB FRAMEWORK AND SERVICES 23


Krish Nandasana TY-BCA - C ROLL NO. - 29

<body>

<h2>Login Status</h2>

<?php

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

$username = $_POST["username"];

$password = $_POST["password"];

$correctUsername = "admin";

$correctPassword = " godsgift ";

if ($username === $correctUsername && $password === $correctPassword) {

echo "<p>Welcome, $username!</p>";

} else {

echo "<p>Error: Invalid username or password. Please try again.</p>";

echo "<p><a href='18-1.html'>Go back to login page</a></p>";

?>

</body>

</html>

WEB FRAMEWORK AND SERVICES 24


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q19 .Create the pages for fruits and vegetables, write the program to display the selected
item in separate page using cookies

Code =

19-1.html

<!DOCTYPE html>

<html>

<head>

<title>Fruits Page</title>

</head>

<body>

<h2>Fruits Page</h2>

<form method="POST" action="19-2.php">

<label for="item">Select a fruit:</label>

<select name="item" id="item">

<option value="Apple">Apple</option>

<option value="Banana">Banana</option>

<option value="Orange">Orange</option>

</select><br><br>

<button type="submit">Submit</button>

</form>

</body>

</html>

19-2.php

<!DOCTYPE html>

<html>

<head>

<title>Vegetables Page</title>

</head>

WEB FRAMEWORK AND SERVICES 25


Krish Nandasana TY-BCA - C ROLL NO. - 29

<body>

<h2>Vegetables Page</h2>

<form method="POST" action="19-3.php">

<label for="item">Select a vegetable:</label>

<select name="item" id="item">

<option value="Carrot">Carrot</option>

<option value="Broccoli">Broccoli</option>

<option value="Spinach">Spinach</option>

</select><br><br>

<button type="submit">Submit</button>

</form>

</body>

</html>

19-3.php

<!DOCTYPE html>

<html>

<head>

<title>Selected Item</title>

</head>

<body>

<h2>Selected Item</h2>

<?php

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

$selectedItem = $_POST["item"];

setcookie("selected_item", $selectedItem, time() + 3600);

echo "<p>You selected: $selectedItem</p>";

}
?>

<p><a href="fruits.html">Go back to fruits</a></p>

WEB FRAMEWORK AND SERVICES 26


Krish Nandasana TY-BCA - C ROLL NO. - 29

<p><a href="vegetables.html">Go back to vegetables</a></p>

</body>

</html>

WEB FRAMEWORK AND SERVICES 27


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q20. Create the pages for fruits and vegetables with their rates, no. of qty, write the
program to calculate the subtotal & total display the selected item and subtotal & total
display in separate page using cookies .

Code =

File 1

<!DOCTYPE html>

<html>

<head>

<title>Fruits Page</title>

</head>

<body>

<h2>Fruits Page</h2>

<form method="POST" action="20-2.php">

<label for="item">Select a fruit:</label>

<select name="item" id="item">

<option value="Apple">Apple</option>

<option value="Banana">Banana</option>

<option value="Orange">Orange</option>

</select><br><br>

<label for="quantity">Quantity:</label>

<input type="number" name="quantity" id="quantity" min="1" required><br><br>

<label for="rate">Rate:</label>

<input type="number" name="rate" id="rate" step="0.01" required><br><br>

<button type="submit">Calculate</button>

</form>

</body>

</html>

WEB FRAMEWORK AND SERVICES 28


Krish Nandasana TY-BCA - C ROLL NO. - 29

20-2.php

<!DOCTYPE html>

<html>

<head>

<title>Vegetables Page</title>

</head>

<body>

<h2>Vegetables Page</h2>

<form method="POST" action="20-3.php">

<label for="item">Select a vegetable:</label>

<select name="item" id="item">

<option value="Carrot">Carrot</option>

<option value="Broccoli">Broccoli</option>

<option value="Spinach">Spinach</option>

</select><br><br>

<label for="quantity">Quantity:</label>

<input type="number" name="quantity" id="quantity" min="1" required><br><br>

<label for="rate">Rate:</label>

<input type="number" name="rate" id="rate" step="0.01" required><br><br>

<button type="submit">Calculate</button>

</form>

</body>

</html>

20-3.php

<!DOCTYPE html>

<html>

<head>

<title>Selected Item and Total</title>

WEB FRAMEWORK AND SERVICES 29


Krish Nandasana TY-BCA - C ROLL NO. - 29

</head>

<body>

<h2>Selected Item and Total</h2>

<?php

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

$selectedItem = $_POST["item"];

$quantity = $_POST["quantity"];

$rate = $_POST["rate"];

$subtotal = $quantity * $rate;

setcookie("subtotal", $subtotal, time() + 3600); // Set a cookie with the subtotal

if (isset($_COOKIE["total"])) {

$total = $_COOKIE["total"] + $subtotal;

} else {

$total = $subtotal;

setcookie("total", $total, time() + 3600); // Set a cookie with the total

echo "<p>Selected Item: $selectedItem</p>";

echo "<p>Quantity: $quantity</p>";

echo "<p>Subtotal: $subtotal</p>";

echo "<p>Total: $total</p>";

?>

<p><a href="fruits.html">Go back to fruits</a></p>

<p><a href="20-1.html">Go back to vegetables</a></p>

</body>

</html>

WEB FRAMEWORK AND SERVICES 30


Krish Nandasana TY-BCA - C ROLL NO. - 29

WEB FRAMEWORK AND SERVICES 31


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q21. Create the login web page using session

Code =

File 1

<!DOCTYPE html>

<html>

<head>

<title>Login Page</title>

</head>

<body>

<h2>Login Page</h2>

<form method="POST" action="21-2.php">

<label for="username">Username:</label>

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

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

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

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

</form>

</body>

</html>

21-2.php

<?php

session_start();

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

$username = $_POST["username"];

$password = $_POST["password"];

// Replace with your actual username and password

$correctUsername = "admin";

WEB FRAMEWORK AND SERVICES 32


Krish Nandasana TY-BCA - C ROLL NO. - 29

$correctPassword = "password";

if ($username === $correctUsername && $password === $correctPassword) {

$_SESSION["username"] = $username;

header("Location: 21-3.php");

exit();

} else {

header("Location: 21-4.php");

exit();

?>

21-3.php

<?php

session_start();

if (!isset($_SESSION["username"])) {

header("Location: 21-3.php");

exit();

?>

<!DOCTYPE html>

<html>

<head>

<title>Welcome</title>

</head>

<body>

<h2>Welcome, <?php echo $_SESSION["username"]; ?>!</h2>

<p><a href="21-4.php">Logout</a></p>

</body>

</html>

21-4.php

WEB FRAMEWORK AND SERVICES 33


Krish Nandasana TY-BCA - C ROLL NO. - 29

<?php

session_start();

session_unset();

session_destroy();

header("Location: 21-1.html");

exit();

?>

WEB FRAMEWORK AND SERVICES 34


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q22. Create the page to get the employee personal informaƟon and store the informaƟon
in mysqldb.

Code =

File 1

<!DOCTYPE html>

<html>

<head>

<title>Employee Information</title>

</head>

<body>

<h2>Employee Information</h2>

<form method="POST" action="22-2.php">

<label for="first_name">First Name:</label>

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

<label for="last_name">Last Name:</label>

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

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

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

<label for="phone">Phone:</label>

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

<button type="submit">Submit</button>

</form>

</body>

</html>

22-2.php

<?php

$hostname = "localhost:3307";

$username = "root";

$password = "";

WEB FRAMEWORK AND SERVICES 35


Krish Nandasana TY-BCA - C ROLL NO. - 29

$dbname = "test22";

$conn = new mysqli($hostname, $username, $password, $dbname);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

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

$first_name = $_POST["first_name"];

$last_name = $_POST["last_name"];

$email = $_POST["email"];

$phone = $_POST["phone"];

$sql = "INSERT INTO emp (first_name, last_name, email, phone)

VALUES ('$first_name', '$last_name', '$email', '$phone')";

if ($conn->query($sql) === TRUE) {


echo "Employee informaƟon saved successfully!";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$conn->close();

?>

WEB FRAMEWORK AND SERVICES 36


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q23 Write a PHP script which will display the copyright information in the following
format. To get current year you can use the date() function. Expected Format : © 2013
PHP Exercises24.

Code =

<?php

$currentYear = date("Y");

$websiteName = "PHP Exercises24";

echo "© $currentYear $websiteName";

?>

WEB FRAMEWORK AND SERVICES 37


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q24. Write a PHP script to print the current date in the following format. To get current
date's information you can use the date() function. Sample format : (assume current date
is September 01, 2013)2013/09/0113.09.0101-09-13

Code=

<?php

$currentDate = date("Y/m/d");

$shortYear = date("y");

$monthWithLeadingZero = date("m");

$dayWithLeadingZero = date("d");

echo "$currentDate$shortYear.$monthWithLeadingZero.$dayWithLeadingZero-

$monthWithLeadingZero-$shortYear";

?>

WEB FRAMEWORK AND SERVICES 38


Krish Nandasana TY-BCA - C ROLL NO. - 29

Q25 .create your own web page using session, database & date concepts

Code =

File 1

<!DOCTYPE html>

<html>

<head>

<title>Login</title>

</head>

<body>

<h2>Login</h2>

<form method="POST" action="25-2.php">

<label for="username">Username:</label>

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

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

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

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

</form>

</body>

</html>

25-2.php

<?php

session_start();

$hostname = "localhost:3307";

$db_username = "root";

$db_password = "";

$dbname = "mystu";

$conn = new mysqli($hostname, $db_username, $db_password, $dbname);

WEB FRAMEWORK AND SERVICES 39


Krish Nandasana TY-BCA - C ROLL NO. - 29

if ($conn->connect_error) {

die("ConnecƟon failed: " . $conn->connect_error);

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

$username = $_POST["username"];

$password = $_POST["password"];

$sql = "SELECT id FROM user WHERE username = '$username' AND password = '$password'";

$result = $conn->query($sql);

if ($result->num_rows === 1) {

$_SESSION["username"] = $username;

header("LocaƟon: 25-2.php");

exit();

} else {

header("Location: 25-3.php");

exit();

$conn->close();

?>

25-3.php

File3

<?php

session_start();

if (!isset($_SESSION["username"])) {

header("Location: login.html");

exit();

?>

<!DOCTYPE html>

<html>

WEB FRAMEWORK AND SERVICES 40


Krish Nandasana TY-BCA - C ROLL NO. - 29

<head>

<title>Welcome</title>

</head>

<body>

<h2>Welcome, <?php echo $_SESSION["username"]; ?>!</h2>

<p>Current Date: <?php echo date("Y-m-d"); ?></p>

<p><a href="25-4.php">Logout</a></p>

</body>

</html>

25-4.php

<?php

session_start();

session_unset();

session_destroy();

header("Location: 25-1.html");

exit();

?>

WEB FRAMEWORK AND SERVICES 41

You might also like