Horje
How to Login Without Database
  1. Login without using the database.
  2. Username and Password stored in PHP array.
  3. The ideal process to protect small pages.

Create HTML Login form

Copy and Paste Following code to index.html This will be your Login form where, you will put username and password
index.html
Example: HTML
<form action="login.php" method="post" name="Login_Form">
  <table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
    <?php if(isset($msg)){?>
    <tr>
      <td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
    </tr>
    <?php } ?>
    <tr>
      <td colspan="2" align="left" valign="top"><h3>Login</h3></td>
    </tr>
    <tr>
      <td align="right" valign="top">Username</td>
      <td><input name="Username" type="text" class="Input"></td>
    </tr>
    <tr>
      <td align="right">Password</td>
      <td><input name="Password" type="password" class="Input"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Submit" type="submit" value="Login" class="Button3"></td>
    </tr>
  </table>
</form>

Create PHP File for Login Check User and Password

Copy and Paste Following code to login.php In this code, We add Username and Password. You may change according to you. Change: Pass: 123456 Username:username1
login.php
Example: PHP
<?php session_start(); /* Starts the session */
        
        /* Check Login form submitted */        
        if(isset($_POST['Submit'])){
                /* Define username and associated password array */
                $logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');
                
                /* Check and assign submitted Username and Password to new variable */
                $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
                $Password = isset($_POST['Password']) ? $_POST['Password'] : '';
                
                /* Check Username and Password existence in defined array */            
                if (isset($logins[$Username]) && $logins[$Username] == $Password){
                        /* Success: Set session variables and redirect to Protected page  */
                        $_SESSION['UserData']['Username']=$logins[$Username];
                        header("location:welcome.php");
                        exit;
                } else {
                        /*Unsuccessful attempt: Set error message */
                        $msg="<span style='color:red'>Invalid Login Details</span>";
                }
        }
?>

Now, Create Login redirect Page

Copy and Paste Following code to welcome.php After login, It will appear Login Session Page. Put following code in any php file where you want to show Login Session. <?php session_start(); ?>
welcome.php
Example: PHP
<?php session_start(); /* Starts the session */

if(!isset($_SESSION['UserData']['Username'])){
        header("location:login.php");
        exit;
}
?>

Congratulation! You have logged into password protected page. <a href="logout.php">Click here</a> to Logout.

Finally, Create a pgae to logout

Copy and Paste Following code to logout.php It will break your login session when You will click on logout.php
logout.php
Example: PHP
<?php session_start(); /* Starts the session */
session_destroy(); /* Destroy started session */
header("location:login.php");  /* Redirect to login page */
exit;
?>
Reffered: w3schools.in





Related Articles
How to Login Without Database PHP Login Tutorial

Single Articles
Create HTML Login formPHP Login Tutorial
Create PHP File for Login Check User and PasswordPHP Login Tutorial
Now, Create Login redirect PagePHP Login Tutorial
Finally, Create a pgae to logoutPHP Login Tutorial

Read Full:
PHP Login Tutorial
Category:
Web Tutorial
Sub Category:
PHP Login Tutorial
Uploaded:
11 months ago
Uploaded by:
Admin
Views:
35
Tested on:
PHP 7



Share on: