Jump to content

[SOLVED] Password Protect PHP adding a second user?


aiden857

Recommended Posts

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

}

?>

Link to comment
Share on other sites

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)) {

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

// 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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.