Password Protect PHP adding a second user?

Discussion in 'PHP' started by aiden857, May 12, 2007.

  1. #1
    Hi all can any one look at this. I have a simple username & password script that i downloaded. it works fine but i need to add asecond user can you tell me how.

    CHEERS.

    Here is the code i have already.

    <?php

    // Define your username and password
    $username = "someuser";
    $password = "somepassword";

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

    ?>

    <h1>Login</h1>

    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>

    <input type="text" title="Enter your Username" name="txtUsername" /></p>

    <p><label for="txtpassword">Password:</label>

    <input type="password" title="Enter your password" name="txtPassword" /></p>

    <p><input type="submit" name="Submit" value="Login" /></p>

    </form>

    <?php

    }
    else {

    ?>

    <p>This is the protected page. Your private content goes here.</p>

    <?php

    }

    ?>
     
    aiden857, May 12, 2007 IP
  2. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Second user what do you mean please describe it properly we are not getting you.
     
    Subikar, May 12, 2007 IP
  3. aiden857

    aiden857 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I need for more than 0ne user name & password to be accepted in the log in fields.
     
    aiden857, May 12, 2007 IP
  4. e39m5

    e39m5 Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This code is untested, but it will support 1, 2, or more than 2 users. You can add users in the $users variable.

    
    <?php
    $users = array('someuser1', 'somepass1', 'someuser2', 'somepass2');
    for($i=0;$i<count($users);$i++){
        $user = $users[$i];
        $pass = $users[$i+=1];
        if ($_POST['txtUsername'] != $user || $_POST['txtPassword'] != $pass) {
            $go = "yes";
            break;
        }
    }
    if($go != "yes"){
    ?>
    <h1>Login</h1>
    
    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
    
    <input type="text" title="Enter your Username" name="txtUsername" /></p>
    
    <p><label for="txtpassword">Password:</label>
    
    <input type="password" title="Enter your password" name="txtPassword" /></p>
    
    <p><input type="submit" name="Submit" value="Login" /></p>
    
    </form>
    <?php
    } else {
    ?>
    <p>This is the protected page. Your private content goes here.</p>
    <?php
    }
    ?>
    
    PHP:
    e39m5
     
    e39m5, May 12, 2007 IP
  5. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or to simplify a bit:
    <?php
    
    // Define your username and password
    $whiteList = array(
        'someuser'    =>    'somepass',
        'anotheruser' =>    'password',
    );
    
    if ( ! array_key_exists($_POST['txtUsername'],$whiteList) || $_POST['txtPassword'] != $whiteList[$_POST['txtUsername']] ) {
    
    ?>
    PHP:
    Obviously the rest doesn't need changing.
     
    rodney88, May 12, 2007 IP