Jump to content

[SOLVED] Multiple Passwords. Do i use arrays?


healy787

Recommended Posts

Hi guys. I have set up a script to allow access to a hidden page. The script works fine however i want to have the option of adding multiple users.I don't want to use Mysql unless i really have to. Ive heard it maybe possible with arrays? Anyway here is the current script.  Thanks very much.

 

<?

$user_name = "user";

$password = "password";

 

$login_user_name = $_POST["login_user_name"];

$login_password = $_POST["login_password"];

$logged_in_for = $_POST["logged_in_for"];

 

if($login_user_name != $user_name || $login_password != $password) {

header("Location: login.php?message=Invalid+user+name+or+password.");

exit;

}

if($login_user_name != $user_name1 || $login_password != $password1) {

header("Location: login.php?message=Invalid+user+name+or+password.");

exit;

}

else {

setcookie("logged_in", $login_user_name, time()+60*60*24*$logged_in_for, "/");

header("Location: index.php");

exit;

}

 

else {

setcookie("logged_in", $login_user_name, time()+60*60*24*$logged_in_for, "/");

header("Location: index.php");

exit;

}

?>

The max number would be limited by your ability to do database's job by hand. You would have to manually keep track of all users and their passwords.

 

 

$userArray = array(
"username1" => "password1",
"username2" => "password2"
);

$login_user_name = $_POST["login_user_name"];
$login_password = $_POST["login_password"];
$logged_in_for = $_POST["logged_in_for"];

if (isset($userArray[$login_user_name])) {
//found user in array, check password
if ($userArray[$login_user_name] == $login_password) {
   //password correct, do login
} else {
   //password incorrect, login failed
}
} else {
  //user not found
}

Hi

 

If you really don't want to use a database and do it simply then I would be tempted to have an array of the passwords keyed on the user name.

 

<?php
$user_name = "user";
$password = array("user" => "password", "user1" => "password1");

$login_user_name = $_POST["login_user_name"];
$login_password = $_POST["login_password"];
$logged_in_for = $_POST["logged_in_for"];

if($password[$login_user_name] != $password) 
{
	header("Location: login.php?message=Invalid+user+name+or+password.");
	exit;
}
else 
{
	setcookie("logged_in", $login_user_name, time()+60*60*24*$logged_in_for, "/");
	header("Location: index.php");
	exit;
}
?>

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.