• home
  • forum
  • my
  • kt
  • download
  • Password Protect a Page Using PHP

    Author: 2008-08-16 11:03:57 From:

    Page Code:

    CODE
    <html>

    <head>
    <title>Secured Area</title>
    </head>

    <body>
    <?php

    // Define your username and password
    $username = "user";
    $password = "password";

    if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

    ?>
    <p>
    <b>Login</b>
    </p>
    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <div align="center">
    <center>
    <table border="0" cellpadding="5" cellspacing="0">
    <tr>
    <td><label for="txtUsername">Username</label>
    </td>
    <td><input type="text" title="Enter your Username" name="txtUsername" /></td>
    </tr>
    <tr>
    <td><label for="txtpassword">Password</label>
    </td>
    <td><input type="password" title="Enter your password" name="txtPassword" /></td>
    </tr>
    </table>
    </center>
    </div>
    <p align="center"><input type="submit" name="Submit" value="Login" /></p>
    </form>

    <?php
    }
    else {
    ?>

    <p>Place your secret content here.</p>

    <?php
    }
    ?>

    </body>

    </html>



    What we have done is made 2 variables - user and password.
    These will be the login details needed to access the protected page.
    What then happens is we set the form to open the content if these variables are the same, otherwise access is denied.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (13)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot