Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    PHP Protect Page Multiple Passwords

    This is a terrific password protection script written by Tom Haigh over at StackOverflow. However, it only allows for a single password - what if I want to use multiple passwords for different folks? Do I need to set up an array? Side note: If I wanted to use a stronger hash than SHA1, can I simply replace "sha1" in the code?

    PHP Code:
    <?php

    $password 
    'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d';

    session_start();
    if (!isset(
    $_SESSION['loggedIn'])) {
        
    $_SESSION['loggedIn'] = false;
    }

    if (isset(
    $_POST['password'])) {
        if (
    sha1($_POST['password']) == $password) {
            
    $_SESSION['loggedIn'] = true;
        echo (
    "Success!");
        } else {
            
    header("Location: failure.php");
        die();
        }


    if (!
    $_SESSION['loggedIn']): ?>

    Log In:

    <form method="post">
        <input type="password" name="password">
        <input type="submit" name="submit" value="Login">
    </form>

    <?php
    endif;
    ?>

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    In the case of multiple people... you may as well go the database route. Use a database like MySQL, or Postgres or whatever you want and create a Users table. Have them register and let them set their own passwords and then when they attempt to login you can run a SQL query to verify that the password matches with the username specified.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yeah, that's a good point. But just for the sake of curiosity, if I didn't want a database, and instead had 3 or 4 users total ... what would I need to modify?

  • #4
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    In that case yeah... An array would do
    Code:
    $password = array('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', 'generate more hashes', 'put them here');
    Then down here I suppose you could put

    Code:
    if (in_array(sha1($_POST['password']), $password)) {
    Of course your script won't be very flexible and easy to maintain this way if it gets large.


  •  

    Tags for this Thread

    Posting Permissions

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