KEMBAR78
PHP Program | PDF | Login | Php
0% found this document useful (0 votes)
9 views28 pages

PHP Program

Uploaded by

Aijt
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)
9 views28 pages

PHP Program

Uploaded by

Aijt
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/ 28

1.

GET NAME OF USER FROM A FORM AND SHOW GREETING TEXT


Program code:
<!DOCTYPE html>
<html>
<body>
<h3> HTML input form </h3>
<!-- HTML form -->
<form method="POST">

<h4>Please enter your First Name : </h4>


<input type="text" name="f_name"><br>
<h4>Please enter your Last Name : </h4>
<input type="text" name="l_name"><br><br>
<input type="submit" value="Display" name="submit">
</form>

</body>
</html>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];

echo "<h1><i> Good Morning, $f_name $l_name </i></h1>";


}
?>
OUTPUT
2.CHECK WHETHER GIVEN STRING IS PALINDROME OR NOT

Program code:
<form method="post">
Enter a String: <input type="text" name="str"/><br>
<button type="submit">Check</button>
</form>
<?php

if($_POST)
{
$str = $_POST['str'];
$reverse = strrev($str); if($str == $reverse)
{
echo "The String $str is palindrome";

}
else
{
echo "The String $str is not palindrome";
}
}

?>
OUTPUT:
3. CHECK WHETHER GIVEN NUMBER IS ARMSTORNG OR NOT

Program code:
<?php
if(isset($_POST['submit']))
{
$number = $_POST['num']; // get the number entered by user
$temp = $number;

$sum = 0;
while($temp != 0 )
{
$remainder = $temp % 10; //find reminder
$sum = $sum + ( $remainder * $remainder * $remainder );
$temp = $temp / 10;

}
if( $number == $sum )
{
echo "$number is an Armstrong Number";
}else
{

echo "$number is not an Armstrong Number";


}
}
?>
<!DOCTYPE html>
<html>

<head>
<title>Whether a number Armstrong or not</title>
</head>
<body>
<form name="armstrong" action="" method="post">
Number :<input type="text" name="num" value="" required=""><br>

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


</form>
</body>
</html>

OUTPUT:
4. PHP PROGRAM USING FUNCTION
Program Code:
<?php
function sayHello($name,$age)
{
echo "Hello $name, you are $age years old<br/>";
}
sayHello("Sonoo",27);

sayHello("Vimal",29);
sayHello("John",23);
?>

OUTPUT:
5. CREATE A PHP PAGE FOR LOGIN PAGE WITHOUT SQL CONNECTION
Program Code:

Login.php
<?php
$username = 'admin';
$password = 'password';

if ($_SERVER['REQUEST_METHOD'] == 'POST')

{
$input_username = $_POST["username"];
$input_password = $_POST['password'];
if ($input_username == $username && $input_password == $password)
{
header('Location: dashboard.php');

exit;
}
else
{
$error_message = 'Invalid username or password';
}

}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>

<style>
body
{
font-family: Arial, sans-serif;
}
.login-form

{
width: 300px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;

box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);


}
</style>
</head>
<body>
<div class="login-form">

<h2>Login</h2>
<?php if (isset($error_message)) : ?>
<p style="color: red;"><?php echo $error_message; ?></p>
<?php endif; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="username">Username:</label>

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


<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</div>

</body>
</html>
Dashboard.php

<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>

<h1>Welcome,
<?php
echo $_POST['username'];
?>
<p>You have successfully logged in.</p></h1>?>
<a href="login.php">Logout</a>

</body>
</html>

OUTPUT:
6. ARRAY MANIULATION
Program Code:
<?php
// Create an array
$numbers = array(10, 20, 30, 40, 50);
// Display original array
echo "<br>Original Array";
print_r($numbers);

// 1. Add element to the end of the array


array_push($numbers, 60);
echo "<br>Array after adding 60";
print_r($numbers);
echo"\n";

// 2. Remove the last element from the array


$last_number = array_pop($numbers);
echo "<br>Removed number: $last_number";
echo "Array after removal:\n";
print_r($numbers);

// 3. Add element to the beginning of the array


array_unshift($numbers, 5);
echo "<br>Array after adding 5 at the beginning:";
print_r($numbers);

// 4. Remove the first element from the array


$first_number = array_shift($numbers);
echo "<br>Removed number: $first_number";
echo "Array after removal:\n";
print_r($numbers);
// 5. Merge two arrays
$more_numbers = array(70, 80, 90);
$all_numbers = array_merge($numbers, $more_numbers);
echo "<br>Merged Array:";
print_r($all_numbers);

// 6. Sort array in ascending order


sort($all_numbers);
echo "<br>Sorted Array (ascending):";
print_r($all_numbers);

// 7. Sort array in descending order


rsort($all_numbers);
echo "<br>Sorted Array (descending)";

print_r($all_numbers);

OUTPUT:
7. DESIGN PERSONAL INFORMATION
Program Code:

<html>
<body>
<form action="" method="POST">
<H1>Biodata</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>";

}
?>
8. LOGIN PAGE WITH SQL CONNECTION

Program Code:

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["email"];
$password = $_POST["password"];
$sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
header("Location: dashboard.php");
exit;
} else {
$error_message = "Invalid email or password";
}

$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
}

.login-form {
width: 300px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

</style>
</head>
<body>
<div class="login-form">
<h2>Login</h2>
<?php if (isset($error_message)) : ?>
<p style="color: red;"><?php echo $error_message; ?></p>
<?php endif; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
dashboard.php (create a new file)
<?php
?>

<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h1>Welcome, <?php echo $_SESSION['email']; ?>!</h1>
<p>You have successfully logged in.</p>
<a href="logout.php">Logout</a>
</body>
</html>

logout.php (create a new file)


<?php
session_start();
session_destroy();
header("Location: login.php");
exit;
?>
OUTPUT:
9. Create a web page to advertise a product of the company using images and audio

Program Code:
<!DOCTYPE html>
<html>
<head>
<title>Product Advertisement</title>
<style>

body {
font-family: Arial, sans-serif;
background-color: pink;
}
.container {
width: 80%;

margin: 40px auto;


text-align: center;
}
.product-image {
width: 50%;
height: 300px;

border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); }
.product-details {
margin-top: 20px;
}
.audio-player {

margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">

<h1>Introducing the X500 Smartwatch</h1>


<img class="product-image" src="x500-smartwatch.jpg" alt="X500 Smartwatch">
<div class="product-details">
<h2>Features:</h2>
<ul>
<li>Heart rate monitoring</li>

<li>GPS tracking</li>
<li>Water resistant up to 50m</li>
<li>Long-lasting battery life</li>
</ul>
<h2>Price: $299.99</h2>
<button>Buy Now</button>

</div>
<div class="audio-player">
<audio controls>
<source src="x500-audio.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>

</div>
</div>
<script>
// Play audio automatically
document.addEventListener("DOMContentLoaded", function () {
document.querySelector("audio").play();

});
</script>
</body>
</html>
OUTPUT:
10. LOGIN SYSTEM USING SESSION
Program code:

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["email"];
$password = $_POST["password"];
$sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
session_start();
$_SESSION["email"] = $email;
$_SESSION["logged_in"] = true;
header("Location: dashboard.php");
exit;
} else {
$error_message = "Invalid email or password";
}

$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
}

.login-form {
width: 300px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

</style>
</head>
<body>
<div class="login-form">
<h2>Login</h2>
<?php if (isset($error_message)) : ?>
<p style="color: red;"><?php echo $error_message; ?></p>
<?php endif; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
dashboard.php
<?php
session_start();
if (!isset($_SESSION["logged_in"]) || $_SESSION["logged_in"] !== true) {
header("Location: login.php");
exit;
}

?>

<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h1>Welcome, <?php echo $_SESSION["email"]; ?>!</h1>
<p>You have successfully logged in.</p>
<a href="logout.php">Logout</a>
</body>
</html>
logout.php
<?php
session_start();
unset($_SESSION["email"]);
unset($_SESSION["logged_in"]);
session_destroy();
header("Location: login.php");
exit;
?>
OUTPUT:

You might also like