Jump to content

[SOLVED] 401 With Multiple Logins


TutorMe

Recommended Posts

I use the following script to create a 401 Unauthorized access page.

<?php
$user = "bob";
$pass = "bob";
if (!(isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']) &&
$_SERVER['PHP_AUTH_USER'] == $user &&
$_SERVER['PHP_AUTH_PW'] == $pass)) {
header('WWW-Authenticate: Basic Realm="Secured Area"');
header('Status: 401 Unauthorized');
} else {
echo "You are in!";
}
?>

How would I go about having multiple logins, and being able to add new logins via another script?

 

Edit:  I'd eventually like to convert this to work with a mysql database.

Link to comment
Share on other sites

try this

 

<?php
$users = array();
$users["bob1"] = "bobpass1";
$users["bob2"] = "bobpass2";
$users["bob3"] = "bobpass3";
$users["bob4"] = "bobpass4";

$valid = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{
if(array_key_exists($_SERVER['PHP_AUTH_USER'],$users)) 
{
	$valid = ($users[$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']);
}
}

if(!$valid)
{
header('WWW-Authenticate: Basic Realm="Secured Area"');
header('Status: 401 Unauthorized');
} else {
echo "You are in!";
}

?>

 

 

**untested

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.