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
https://forums.phpfreaks.com/topic/82866-solved-401-with-multiple-logins/
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

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.