/    Sign up×
Community /Pin to ProfileBookmark

Password Protect PHP adding a second user?

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

}

?>

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SheldonMay 12.2007 — try this...
[code=php]
<?php

if(!empty($_POST['txtUsername'])){
$username = $_POST['txtUsername'];
$password = $_POST['txtpassword'];

if((($username == "user1";) and ($password == "user1spassword")) or
(($username == "user2";) and ($password == "user2spassword")) or
(($username == "user3";) and ($password == "user3spassword")){
echo("Welcome {$username}, you have successfully logged in.";
}else{
echo("Please enter a correct username<a href=".basename(htmlentities($_SERVER['PHP_SELF']))).">Try again</a>");
}else{
?>
<h1>Login</h1>

<form name="form" method="post" action="<?php echo(basename(htmlentities($_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 } ?>
[/code]
Copy linkTweet thisAlerts:
@patenaudematMay 12.2007 — This might be a little simpler:

[code=php]
<?php

// Define your usernames and passwords
$users = array('someUser1', 'someUser2', 'someUserN');
$passwords = array('password1', 'password2', 'passwordN');

if ( ($user = array_search($_POST['txtUsername'], $users)) !== false && $_POST['txtPassword'] == $passwords[$user] ) {

?>

<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 } ?>
[/code]


It's a little complex, but a little tighter. Requires PHP >= 4.2.0.

Hope it helps!

-Matt
Copy linkTweet thisAlerts:
@HazardTWJun 02.2007 — How secure is a PHP page that requires a password?

Basically what I am asking is if the only two ways a person can see the content of a pw protected php page is by either getting the pw correct, or having ftp access to your webserver?
Copy linkTweet thisAlerts:
@SheldonJun 03.2007 — Yep, a decent PHP auth script can keep out every one who doesnt no the password. FTP auth is a lot different.
×

Success!

Help @aiden857181 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...