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 } ?>
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
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.