CrazyEngineers
  • WebDev: Simple Login Tutorial Using PHP. (DB Less Version)

    PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92
    Updated: Oct 13, 2024
    Views: 1.1K
    Hi Friends,
    Today I would like to say how to create a login system using PHP, without using Databases.

    We need these files:
    1. index.php - Index file, where your protected content and login form is there.
    2. login.php - Login Form.
    3. logout.php - Script for logging out.

    Code: index.php
    <?php
    session_start
    (); // Initiate the session
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="https://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Welcome</title>
        <style type="text/css">
            * {font-family: Tahoma; font-size: 12px;}
            a {color: #0000ff; text-decoration: none;}
            a:hover {color: #ff0000;}
        </style>
    </head>

    <body>
        <?php
        
    if(isset($_SESSION["logged"])) // Check if the user had logged in.
            
    echo 'Welcome! [url=logout.php]Logout[/url].';
        else 
    // User did not log in.
            
    echo 'Please [url=login.php]Login[/url].';
        
    ?>
    </body>
    </html>
    Code: login.php
    <?php
    session_start
    (); // Initiate the session.
    $err false// Set an Error Flag.
    if(count($_POST))
        if(isset(
    $_POST["username"], $_POST["password"])) // See if the user has entered both username and password.
            
    if($_POST["username"]=="Admin" && $_POST["password"]=="LetMeIn"// Check for the correctness of both username and password.
            
    {
                
    $_SESSION["logged"] = true// Set the session as authenticated.
                
    header('Location: index.php'); // Redirect the user to the home page.
                
    die(); // Stop the script.
            
    }
            else
                
    $err true// Username or password is wrong! Set error flag.
        
    else
            
    $err true// User didn't provide both username and password.
    else
        
    $err false// User logs in for the first time.
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="https://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Login</title>
        <style type="text/css">
            * {font-family: Tahoma; font-size: 12px;}
            label {display: inline-block; width: 75px; margin: 5px 0px; padding: 5px;}
            input {border: 0px; border-bottom: 1px solid #cccccc; width: 100px; margin: 5px 0px; padding: 3px;}
            form {text-align: center;}
        </style>
    </head>

    <body>
        <?php if($err) echo '[b]Invalid Login! Provide all values![/b]<br />'// If the error flag is set, display error message. ?>
        <form method="post">
            <div><label for="username">Username:</label> <input type="text" name="username" /></div>
            <div><label for="password">Password:</label> <input type="password" name="password" /></div>
            <div><input type="submit" value="Login!" /></div>
        </form>
    </body>
    </html>
    Code: index.php
    <?php
        session_start
    (); // Initiate the session
        
    session_destroy(); // Destroy the session
        
    header('Location: index.php'); // Redirect the user to the home page
    ?>
    Now keep all the files in the same folder... Ready to execute?

    Execution
    I personally recommend USB Web Server for fast food execution. Download it at #-Link-Snipped-#
    Now copy the three files in the Root folder of USB Web Server and open the port #-Link-Snipped-# in your favourite browser and enjoy. I have also attached the set of files, which I used with this post. 😀

    All the coding is documented. Even then if you have any queries, do shoot them out in the replies. 😀

    Download the files: #-Link-Snipped-#
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register