Password Protecting a Single File in PHP

Discussion in 'PHP' started by EGS, Apr 22, 2007.

  1. #1
    Hi.

    How can I password-protect a single file using PHP? I want a login prompt to show up utilizing a username and password set by PHP variables to view the web page's contents.

    I don't want it to be able to be viewed by source code or easily h4x0red.

    I want it to be like if a user wants to see 1.php they'll have to enter right login info to view contents.

    Please assist.
     
    EGS, Apr 22, 2007 IP
    commandos likes this.
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    you can :

    - make a form
    - get the variables (username + password)
    - test if the username and password match
    - show the real content


    The form is just simple HTML , In the Form action you put the page it self .

    To get the variable :

    $_POST[user] , $_POST[pass] // if the form method is POST .

    Then you can test :

    $user = "test";
    $pass = "test_";

    if $_POST[user] == $user && $_POST[pass] == $pass ...

    you show the real content ..

    else show the form again with the error message .
     
    commandos, Apr 22, 2007 IP
  3. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    it is also important that you store the restricted file outside of your document root, so that it is not possible to access the file directly (bypassing the PHP script) from the browser.
     
    phper, Apr 22, 2007 IP