Type this in your login page: (login.php)
<?php
// First we start session
session_start();
ob_start()
?>
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('
Basic realm="Only authorized access"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.'; // If the user clicks cancel button
exit;
}
//connection to the database
$link = mysql_connect("localhost", "root", "root") or die("cant connect");
mysql_select_db("db", $link) or die("cant find the database");
$result = mysql_query("SELECT * FROM usuarios", $link);
$ok = false;
while(($row = mysql_fetch_array($result)) && !$ok) {
if (($_SERVER['PHP_AUTH_USER']==$row['user']) && (md5($_SERVER['PHP_AUTH_PW'])==$row['password']))
$ok=true;
$user = $_SERVER['PHP_AUTH_USER'];
}
// Now we see if user has admin features
$result = mysql_query("SELECT * FROM users WHERE user='$user'") or die("cant find the user");
$row = mysql_fetch_array($result);
$feature = $row['feature'];
// And we put this variables on the session
$_SESSION['user'] = $user;
$_SESSION['feature'] = $feature;
?>
/* Note this code uses a database called "db" which contains three tables: user, password and feature (password is encrypted using md5 algorithm) */
Now all your pages (must be *.php) must begin with this:
<?php
session_start();
$user = $_SESSION['user'];
if (!isset($user)) {
header('
Basic realm="Only authorized access"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;
}
$link = mysql_connect("localhost", "root", "root") or die("cant connect");
mysql_select_db("db", $link) or die("cant find database");
$result = mysql_query("SELECT feature FROM users WHERE user='$user'", $link);
$row = mysql_fetch_array($result);
if ($row[0] != "admin") { // this is not necessary if this page is for all users
echo "This is only for admin features";
exit;
}
?>
Hope this helps,
Daniel