aiden857 Posted May 12, 2007 Share Posted May 12, 2007 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> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/ Share on other sites More sharing options...
taith Posted May 12, 2007 Share Posted May 12, 2007 easiest if you use a database... but thats your choice... $username = "someuser"; $password = "somepassword"; $username2 = "someotheruser"; $password2 = "someotherpassword"; if(($_POST['txtUsername']==$username&&$_POST['txtPassword']==$password)||($_POST['txtUsername']==$username2&&$_POST['txtPassword']==$password2)) { Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251438 Share on other sites More sharing options...
lalabored Posted May 12, 2007 Share Posted May 12, 2007 Oops made a mistake. <?php // Define your username and password $loginformation = array("User1" => "Pass1","User2" => "Pass2"); if (!in_array($_POST['txtUsername'], $loginformation) and !array_key_exists($_POST['txtPassword'], $loginformation[$_POST['txtUsername']])) { ?> <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 } ?> Might not work o.o Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251441 Share on other sites More sharing options...
emehrkay Posted May 12, 2007 Share Posted May 12, 2007 // Define your username and password $username = "someuser"; $password = "somepassword"; if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { change to // Define your username and password $username = array('user1, 'user2'); $password = array('pass1', 'pass2'); if (!in_array($_POST['txtUsername'], $username)|| !in_array($_POST['txtPassword'], $password)) { each user/pass pair must match, so user1 and pass1 must go together the code i wrote simply says; if post[txtusername] is not in the username array or post[txtpassword] is not in the password array Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251484 Share on other sites More sharing options...
aiden857 Posted May 12, 2007 Author Share Posted May 12, 2007 Sorr guys thats not working. Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251503 Share on other sites More sharing options...
redbullmarky Posted May 12, 2007 Share Posted May 12, 2007 aiden857 - do NOT post the same topic more than once, never mind 4 times. It's more likely to annoy than to get help, not to mention it being against the forum rules. i've merged them all into this one. in future, please find the most suitable place and post it just once. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251509 Share on other sites More sharing options...
karatekid36 Posted May 12, 2007 Share Posted May 12, 2007 If you plan on having more than two users, I would suggest using a database. Also, if you use a database, it provides a bit more security in regards to how the password is stored. If you are going to go through the trouble to password protect something, you might want to take the extra step and do it in a fashion that makes it a bit more secure. Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251513 Share on other sites More sharing options...
redbullmarky Posted May 12, 2007 Share Posted May 12, 2007 try: <?php // Define your usernames and passwords $users = array( 'someuser' => 'somepassword', 'someuser2' => 'somepassword2' ); $username = $_POST['txtUsername']; $password = $_POST['txtPassword']; if (isset($users[$username]) && ($users[$username] == $password)) { ?> <p>This is the protected page. Your private content goes here.</p> <?php } else { ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> <?php } ?> personally though, it's a pretty awful login script for anything more than extremely simple. Employing a database of some form would be your best bet - there are plenty of tutorials (on both this site and via Google) not to mention the fact that it's much easier to deal with adding more users when the details aren't hard coded into the page. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/51084-solved-password-protect-php-adding-a-second-user/#findComment-251517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.