Password Array

Hello,

I have the script below and I’m trying to get it to work so that there are several valid passwords. The first script below works with the one password:

[code]<?php
$password = “bob”;
?>

<?php print "

"; // If password is valid let the user get access if (isset($_POST["password"]) && ($_POST["password"]=="$password")) { ?>




You are a winner

<?php } else { // Wrong password or no password entered display this message if (isset($_POST['password']) || $password == "") { print "

You lose.

";} print "

Please enter your code below
"; print "

"; } print "

"; ?>
[/code]

But I’m trying to get several password to work. I’m trying to use an array but it ain’t happening. My attempt is below:

[code]<?php
$password = array(“password1”, “password2”, “password3”, “admin”);

?>

<?php print "

"; // If password is valid let the user get access if (isset($_POST["password"]) && ($_POST["password"]=="$password")) { ?>




You are a winner

<?php } else { // Wrong password or no password entered display this message if (isset($_POST['password']) || $password == "") { print "

You lose.

";} print "

Please enter your code below
"; print "

"; } print "

"; ?>
[/code]

any help would be much appreciated.

Thanks
Robert

MOD EDIT: Added code tags

Using ==, you’re only comparing values of the same type. Since you’re storing your passwords in an array, and are comparing it to a string, it will naturally return false. What you’re probably looking for is in_array().

Hi Zyppora,

Thanks for the help. I’m thinking the in_array fucntion has to go around these two places:

if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {

and

if (isset($_POST['password']) || $password == "") { print "<p align="center">not a winner</p>";} print "<form method="post"><p align="center">Please enter your code below<br>"; print "<input name="password" type="password" size="25" maxlength="10"><input value="Login" type="submit"></p></form>"; }

am I’m getting closer?

Thanks,
Robert

You’ll have to change

if (isset($_POST["password"]) && ($_POST["password"]=="$password")) 

into

if (isset($_POST["password"]) && in_array($_POST["password"], $password)) 

works perfectly

thanks for your help - I really appreciate it

-Robert

Sponsor our Newsletter | Privacy Policy | Terms of Service