Example : How to open the Home page after successful validation of the Login Page in PHP?
STEP 0 : CREATE A COMPLETE SIGN UP PAGE FIRST(user_registration.php)
STEP 1 : CREATE A LOGIN PAGE(login.php)
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial;
background: #e6f2ff;
}
.login-box {
width: 350px;
margin: 120px auto;
padding: 30px;
background: white;
border-radius: 10px;
box-shadow: 0px 0px 10px gray;
}
h2 {
text-align: center;
color: #003366;
}
input[type=text],
input[type=password] {
width: 100%;
height: 40px;
margin-top: 8px;
margin-bottom: 18px;
padding-left: 10px;
font-size: 16px;
box-sizing: border-box;
}
.password-box {
position: relative;
}
.password-box input {
padding-right: 40px;
}
.eye-icon {
position: absolute;
right: 10px;
top: 17px;
cursor: pointer;
font-size: 18px;
}
.btn {
width: 48%;
height: 40px;
background: #003366;
color: white;
border: none;
font-size: 17px;
border-radius: 5px;
cursor: pointer;
}
.btn:hover {
opacity: 0.8;
}
.signup {
text-align: center;
margin-top: 20px;
}
.signup a {
color: #003366;
font-weight: bold;
text-decoration: none;
}
.signup a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="login-box">
<h2>User Login</h2>
<form action="logincheck.php" method="post">
<label>Login ID</label>
<input type="text" name="lid1" id="lid2" placeholder="Enter Login ID" required>
<label>Password</label>
<div class="password-box">
<input type="password" name="pss1" id="pss2" placeholder="Enter Password" required>
<span class="eye-icon" onclick="showPassword()">👁</span>
</div>
<input type="submit" value="Login" class="btn">
<input type="reset" value="Reset" class="btn">
<div class="signup">
New User? <a href="user_registration.php">Sign Up</a>
</div>
</form>
</div>
<script>
function showPassword() {
var pass = document.getElementById("pss2");
if (pass.type === "password") {
pass.type = "text";
} else {
pass.type = "password";
}
}
</script>
</body>
</html>
STEP 2 : CREATE LOGIN VALIDATION PAGE(logincheck.php)
<?php
$conn = mysqli_connect("localhost","root","","database_name");
$lid3 = $_POST['lid1'];
$pss3 = $_POST['pss1'];
$sql = "select * from user_registration where lid5='$lid3' and pss5='$pss3'";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if($count > 0)
{
session_start();
$_SESSION['user']=$lid3;
header("Location: home.php");
}
else
{
echo "<script>
alert('Invalid Login ID or Password');
window.location='login.php';
</script>";
}
?>
STEP 3 : OPENS HOME PAGE AFTER SUCCESSFUL VALIDATION(home.php)
<?php
session_start();
if(!isset($_SESSION['user']))
{
header("Location: login.php");
}
?>
<h1>Welcome To Home Page</h1>
<a href="logout.php">Logout</a>
STEP 4 : CREATE LOGOUT PAGE(logout.php)
<?php
session_start();
session_destroy();
header("Location: login.php");
?>
STEP 5 : CREATE USER_REGISTRATION TABLE AND INSERT DATA IN MYSQL
CREATE TABLE user_registration
(
lid5 varchar(50),
pss5 varchar(50)
);
------------------------
INSERT INTO user_registration
VALUES('admin','123');
INSERT INTO user_registration
VALUES('admin','admin');
![]()
0 Comments