Results 1 to 3 of 3

Thread: protecting files

  1. #1
    Join Date
    Mar 2006
    Posts
    600
    Thanks
    5
    Thanked 4 Times in 4 Posts

    Default protecting files

    i need a password protection script that actually works, rather than the javascript stuff that can be figured out. They have these in PHP right...
    Last edited by benslayton; 06-01-2006 at 02:03 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    security.inc.php. Include at the very top of your page(s) with:
    Code:
    <?php require("security.inc.php"); ?>
    Code:
    <?php session_start();
    $password = "your password goes here";
    
    function showLoginForm($badpw = false) {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Login Form</title>
      </head>
      <body>
        <form action="<?php echo($PHP_SELF); ?>" method="post">
          <p>
          <?php if($badpw) { ?>
            The password you entered was incorrect.  Please try again.
          <?php } ?>
            <input type="password" name="pass">
            <input type="submit" value="Log In">
          </p>
        </form>
      </body>
    </html>
    <?php
      return "";
    }
    
    if(!isset($_SESSION['pwhash']) && !isset($_POST['pass']))
      die(showLoginForm());
    else if(isset($_POST['pass']))
      if($_POST['pass'] == $password) $_SESSION['pwhash'] = md5($password);
      else die(showLoginForm(true));
    else if(isset($_SESSION['pwhash']))
      if($_SESSION['pwhash'] != md5($password)) die(showLoginForm(true));
    ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Posts
    600
    Thanks
    5
    Thanked 4 Times in 4 Posts

    Wink

    Thanks twey, that helps alot

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •