Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

php lab program

The document contains multiple PHP scripts demonstrating various functionalities such as greeting users, checking Armstrong numbers, user login without SQL, array manipulation, and designing personal information forms. It also includes a login system using sessions and a product advertisement page. Each section is accompanied by HTML forms for user input and interaction.

Uploaded by

lalithkishore027
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

php lab program

The document contains multiple PHP scripts demonstrating various functionalities such as greeting users, checking Armstrong numbers, user login without SQL, array manipulation, and designing personal information forms. It also includes a login system using sessions and a product advertisement page. Each section is accompanied by HTML forms for user input and interaction.

Uploaded by

lalithkishore027
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

Show greeting text


<?php

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

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

echo "Hello, " . $name . "! Welcome to our website.";

?>

<!-- HTML Form -->

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

Name: <input type="text" name="name">

<input type="submit">

</form>
Output:
3.Check Armstrong number
<?php

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

$number = $_POST['number'];

$sum = 0;

$temp = $number;

$length = strlen($number);

while ($temp != 0) {

$remainder = $temp % 10;

$sum += pow($remainder, $length);

$temp = (int)$temp / 10;

if ($sum == $number) {

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

} else {

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

?>

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

Enter Number: <input type="number" name="number">

<input type="submit">

</form>
Output:
4.Program using function
<?php

function greetUser($name) {

return "Hello, " . $name . "! Welcome to our website.";

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

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

echo greetUser($name);

?>

<!-- HTML Form -->

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

Name: <input type="text" name="name">

<input type="submit">

</form>
Output:
5.Login page without SQL
<?php

$validUsername = "user";

$validPassword = "password";

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

$username = $_POST['username'];

$password = $_POST['password'];

if ($username == $validUsername&& $password == $validPassword) {

echo "Login successful. Welcome, " . $username . "!";

} else {

echo "Invalid username or password.";

?>

<!-- HTML Form -->

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

Username: <input type="text" name="username"><br><br>

Password: <input type="password" name="password"><br><br>

<input type="submit">

</form>
Output:
6.Array manipulation
<?php

$array = [1, 2, 3, 4, 5];

echo "Original Array: ";

print_r($array);

echo "<br>Reversed Array: ";

print_r(array_reverse($array));

echo "<br>Array Sum: " .array_sum($array);

sort($array);

echo "<br>Sorted Array: ";

print_r($array);

?>
Output:
7.Design personal information
<html>

<body>

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

<h1>Biodate</h1>

Name:<input type=text name="name1"<br><br>

Address:<textarea name=address></textarea><br><br>

Age: <input type=text name=age><br><br>

Phone: <input type=text name =phone ><br><br>

Email: <input type=text name=email><br><br>

Educational Qualification: <input type =text name=qualification><br><input type


=submit value= Display><br><br>

</form>

</body>

</html>

<?php

if($_POST)

echo "<h1>Biodata</h1>";

echo "Name :".$_POST["name1"]."<br>";

echo "Address :".$_POST["address"]."<br>";

echo "Age :".$_POST["age"]."<br>";

echo "Mobile :".$_POST["phone"]."<br>";

echo "Email :".$_POST["email"]."<br>";


echo "Qualification :".$_POST["qualification"]."<br>";

?>
Output:
8.Login page with SQL
<?php

$username = $_POST['username'];

$password = $_POST['password'];

$conn = mysqli_connect("localhost","root","","login");

if(mysqli_connect_error())

echo "Failed to conect!";

exit();

$sql = "INSERT INTO login(username,password)


values('$username','$password')";

$r = mysqli_query($conn,$sql);

?>

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

Username: <input type="text" name="username" required><br><br>

Password: <input type="password" name="password" required><br><br>

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

</form>

<?php

if($r)

echo "Welcome !".$username;

}
else

echo "!LOGIN";

?>
Output:
9.Advertise a product
<!DOCTYPE html>

<html lang="en">

<head>

<title>Product Advertisement</title>

</head>

<center>

<body>

<h1>Amazing Product: IPHONE</h1>

<img src="image.jpg" weight="300px" height="400px">

<p>The iPhone 12 Pro Max performs particularly well when recording selfie
videos.</p>

</body>

<audio controls>

<source src="audio.mp3" type="audio/mpeg">

Your browser does not support the audio element.

</audio><br>

<button>Buy Now</button>

</center>

</html>
Output:
10.Login system using session
login.php
<?php

session_start();

$validUsername ="user";

$validPassword="password";

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

$username = $_POST['username'];

$password = $_POST['password'];

if ($username == $validUsername && $password == $validPassword) {

$_SESSION['username'] = $username;

header("Location: dashboard.php");

exit();

} else {

$error = "Invalid username or password";

?>

<!DOCTYPE html>

<html lang="en">

<head>

<title>Login</title>
</head>

<body>

<h1>Login</h1>

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

<p style="color: red;"><?php echo $error; ?></p>

<?php endif; ?>

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

Username: <input type="text" name="username" required><br><br>

Password: <input type="password" name="password" required><br><br>

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

</form>

</body>

</html>

dashboard.php
<?php

session_start();

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

header("Location: login.php");

exit();

?>
<!DOCTYPE html>

<html lang="en">

<head>

<title>Dashboard</title>

</head>

<body>

<h2>Welcome to the Dashboard</h2>

<p>Hello, <?php echo $_SESSION['username']; ?>! You are logged in.</p>

<a href="logout.php">Logout</a>

</body>

</html>

logout.php
<?php

session_start();

session_unset();

session_destroy();

header("Location: login.php");

exit();

?>
Output:

You might also like