Skip to main content
Participant
June 28, 2024
Question

Password log in

  • June 28, 2024
  • 2 replies
  • 303 views

I want to create a log in page on my website to allow only certain users who know the log in and password details to access some areas of the site.   I have copied a php code from elsewhere, but can't get it to work, is anyone able to help please?

    This topic has been closed for replies.

    2 replies

    Nancy OShea
    Community Expert
    Community Expert
    June 28, 2024

    This is a very simple example of a password protected page.

    Copy & paste this code into a new PHP document. 

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Password Protected Content</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <!--minimal demo styles-->
    <style>
    html {font-size:16px}
    
    body {font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, "sans-serif";
    font-size: 1.5rem;
    width:80%;
    margin:0 auto;
    }
    </style>
    </head>
    <body>
    
    
    <?php
    // Define your username and password here
    $username = "admin";
    $password = "pass123";
    
    if (isset($_POST['txtUsername']) != $username || $_POST['txtPassword'] != $password) {
    ?>
    <!--LOG-IN FORM-->
    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
    <legend>Log-In</legend>
    
    <p><label for="txtUsername">Username:</label>
    <br>
    <input type="text" title="Enter your Username" name="txtUsername" id="txtUsername" required placeholder="5 characters"></p>
    
    <p><label for="txtPassword">Password:</label>
    <br>
    <input type="password" title="Enter your password" name="txtPassword" id="txtPassword" required placeholder="7 characters"></p>
    
    <button type="submit" class="btn btn-lg btn-primary">Submit</button>
    </fieldset>
    </form>
    
    <?php
    }
    else {
    ?>
    
    <!--PROTECTED CONTENT-->
    <h2>Log-In Success!</h2>
    <p>You have reached a password protected area of this page. What would you like to do next?</p>
    
    <!--Links to other pages-->
    <ul>
    <li> <a href="download.html">Download Files</a></li>
    <li><a href="#">Upload Files</a></li>
    <li><a href="#">Watch Videos</a></li>
    <li><a href="#">Play a Game</a></li>
    </ul>
    <?php
    }
    ?> 
    <!--END PROTECTED CONTENT-->
    
    </body>
    </html>

     

    SaveAs test.php.

    Test script on a PHP server.  

    ===============

    LOG-IN

    User: admin

    Password: pass123

    ===============

     

    Hope that helps.

     

     

     

    Nancy O'Shea— Product User & Community Expert
    Participant
    July 5, 2024

    thank you

     

    Legend
    June 28, 2024

    See my reply in the post you copied the php code from...........see if that helps.