PHP : Login Form using session without Database and cookies

PHP : Login Form using session without Database and cookies

Today's topic you will learn how to create a simple login form using PHP session and HTML commands without using a database 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 [ session ] command.

What is the function of the session command in PHP?
Use session variables to temporarily store information. This information is safe as long as the browser is open. Session variables can be deleted if the user wants or browser closed.

How to use PHP Session ?
PHP session is started with the session_start() function. This function first checks if the session has already started and if no one has started then it starts. session_start() must be written at the beginning of each pages.

Storing , get and delete session information.
This session command is used to store any information.
$_SESSION['user_name'] = 'itcollegeall'; or $_SESSION["user_name"] = "itcollegeall";

The stored information is get back using this session command.
echo $_SESSION["user_name"]; or echo $_SESSION['user_name'];

This session command is used to delete stored information.
unset($_SESSION["user_name"]); or unset($_SESSION['user_name']);

I have already explained in detail about the Session 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
// start php session
session_start();
if ( isset ( $_POST["login"] ) ) {
// Define $username and $password
$user = $_POST["user"];
$password = $_POST["password"];
if ( $user == "admin" and $password == "password" ) {
// Storing session data
$_SESSION["user_name"] = $user;
$_SESSION["password"] = $password;
}else{
echo "The username and password entered do not match. please try again";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Login successful</h2>
<div>
<label>User Name : <?php echo $_SESSION["user_name"]; ?></label>
</div>
<div>
<label>Password : <?php echo $_SESSION["password"]; ?></label>
</div>
</body>
</html>

The login page has been created.
Check out this login page username [ admin ] and password is [ password ].

Second method

You will need to create three pages to use this method.
The first page will be the login.php.
The second page of the dashboard.php.
The third page of the logout.php.

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
// start php session
session_start();
// If the session is already in store then goto dashboard page
if ( isset ( $_SESSION["user_name"] ) ) {
header("Location: dashboard.php");
}else{
// If the session is not store
if ( isset ( $_POST["login"] ) ) {
// Define $username and $password
$user = $_POST["user"];
$password = $_POST["password"];
if ( $user == "admin" and $password == "password" ) {
// Storing session data
$_SESSION["user_name"] = $user;
$_SESSION["password"] = $password;
header("Location: dashboard.php");
}else{
echo "The username and password entered do not match. please try again";
}
}
?>
<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
}
?>

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

<?php
// start php session
session_start();
// If the session is already in store
if ( !isset ( $_SESSION["user_name"] ) ) {
header("Location: login.php");
}else{
?>
<html>
<head>
</head>
<body>
<h2>Login successful</h2>
<div>
<label>User Name : <?php echo $_SESSION["user_name"]; ?></label>
</div>
<div>
<label>Password : <?php echo $_SESSION["password"]; ?></label>
</div>
<br>
<a href="logout.php">Log Out</a>
</body>
</html>
<?php
}
?>

What is the purpose of isset?
check variable is set or not.

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

<?php
// start a session
session_start();
unset($_SESSION["user_name"]);
unset($_SESSION["password"]);
session_destroy();
echo "Logout successful";
?>
<html>
<head>
</head>
<body>
<a href="login.php">LogIn</a>
</body>
</html>

Share This:

No comments:

Post a Comment

Advertiser
Counter
Flag Counter