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. First methodStep 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 methodYou 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