PHP : Login Form Without Database , session and cookies

PHP : Login Form Without Database , session and cookies

Today's topic you will learn how to create a simple login form using PHP and HTML commands without using a database , session and cookies.
This login form will be very simple. Which will be very easy to make.

The login page was created on this page using the [ IF ] command.

So let's get started.

The login page here explains two method.

First method : Create a login using two pages.
Second method : Create a login using one page.

First method

Step No : 1 Create a page called login.html.
Step No : 2 Now copy and paste this code into login.html.
Step No : 3 Then save.

Code :

<html>
<head>
</head>
<body>
<div>
<h2>LOGIN</h2>
</div>
<form action="login.php" method="post" >
<div>
<label>User Name</label>
<input type="text" name="user" />
</div>
<div>
<label>Password</label>
<input type="password" name="password" />
</div>
<div>
<input type='submit' name="Login" />
</div>
</form>
</body>
</html>

Step No : 4 Create a page called login.php.
Step No : 5 Now copy and paste this code into login.php.
Step No : 6 Then save.

<?php
$user = $_POST["user"];
$password = $_POST["password"];
if ( $user == "admin" and $password == "password" ) {
echo "Login successful";
}else{
echo "The username and password entered do not match. please try again";
}
?>

The login page has been created.

Second method

This way we will create a login form on the same page and check if the user and password are correct.
But to create such a page you need to add more code.
These codes will also be explained here.
Step No : 1 Create a page called login.php.
Step No : 2 Now copy and paste this code into login.php.
Step No : 3 Then save.

<?php
if ( !empty ( $_POST["login"] ) ) {
$user = $_POST["user"];
$password = $_POST["password"];
if ( $user == "admin" and $password == "password" ) {
echo "Login successful";
}else{
echo "The username and password entered do not match. please try again";
}
}else{
?>
<html>
<head>
</head>
<body>
<div>
<h2>LOGIN</h2>
</div>
<form action="login.php" method="post" >
<div>
<label>User Name</label>
<input type="text" name="user" />
</div>
<div>
<label>Password</label>
<input type="password" name="password" />
</div>
<div>
<input type='submit' name="login" />
</div>
</form>
</body>
</html>
<?php
}
?>

Share This:

No comments:

Post a Comment

Advertiser
Counter
Flag Counter