Hey,
I've noticed a few threads where people are asking about creating an auth script or help with one they've created. So i thought this may help some people...
passwd.txt:
admin|password1
elfyn|password2
guest|password
simple_auth.php:
<?php
/*
Read in the user's file (passwd.txt).
*/
$usertmp = file("passwd.txt");
foreach( $usertmp as $ut_key => $value )
{
if( preg_match("/(.*)\|(.*)/",$value,$ut_re) )
{
$users[$ut_key][login] = $ut_re[0];
$users[$ut_key][passwd] = $ut_re[1];
}
}
/*
Declare utility function(s).
*/
function in_multi_array( $needle,$haystack )
{
$in_multi_array = 0;
if( in_array($needle,$haystack) )
{
$in_multi_array = 1;
}
else
{
for( $i=0; $i<sizeof($haystack); $i++ )
{
if( is_array($haystack[$i]) )
{
if( in_multi_array($needle, $haystack[$i]) )
{
$in_multi_array = 1;
break;
}
}
}
}
return $in_multi_array;
}
/*
The Code:
*/
if( $_POST["submit"] )
{
if( $_POST["login"] && $_POST["passwd"] )
{
$user_login = 0;
$user_passwd = 0;
if( in_multi_array($_POST["login"],$users) )
{
$user_login = 1;
}
if( in_multi_array($_POST["passwd"],$users) )
{
$user_passwd = 1;
}
if( $user_login && $user_passwd )
{
echo "Access Granted to user ".$_POST["login"]."!\n";
}
else
{
echo "Access Denied!\n";
}
}
}
else
{
echo "<form method=\"post\">\n";
echo "Username:<br>\n";
echo "<input type=\"text\" name=\"login\"><br>\n";
echo "Password:<br>\n";
echo "<input type=\"password\" name=\"passwd\"><br><br>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Login\">\n";
echo "</form>\n";
}
?>
If your using apache it might be useful to rename the passwd.txt file .htpasswd. And whatever system/OS your using you should try and keep the passwd.txt (or whatever...) outside of your webroot...