PHP Code:
<?php
// File Name: access.php
// Check to see if $PHP_AUTH_USER already contains info
if (!isset($PHP_AUTH_USER)) {
// If empty, send header causing dialog box to appear
header('WWW-Authenticate: Basic realm="Private. Unauthorised use prohibited"');
header('HTTP/1.0 401 Unauthorized');
exit;
} else if (isset($PHP_AUTH_USER)) {
// If not empty, check the database for matches
// connect to MySQL
mysql_connect("myhost", "user", "password")
or die ("Unable to connect to database. Please contact the webmaster");
// select db
mysql_select_db("auth")
or die ("Unable to select database.");
// Query db
$sql = "SELECT *
FROM users
WHERE user_name='$PHP_AUTH_USER' and pass='$PHP_AUTH_PW'";
// Execute the query and put results in $result
$result = mysql_query($sql);
// Get number of rows in $result. 0 if invalid, 1 if valid.
$num = mysql_numrows($result);
if ($num != "0") {
// get real name
$userq*=*"SELECT*real_name
*****FROM*users
*****WHERE*user_name='$PHP_AUTH_USER'*";
// put real name in a variable
$user*=*mysql_query($userq);
//extract data from real name
$row*=*mysql_fetch_array($userq);
// display message
print*"<P><font color=\"purple\" face=\"arial\" size=\"-2\">Welcome, ".$row['real_name']*."</font></p>";
} else {
header('WWW-Authenticate: Basic realm="Private. Unauthorised use prohibited"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required. If you are an authorised user, please contact <b>Me</b> ';
exit ;
}
}
?>
It's used as an include in several php files on the site (or at least that's the intention!). Thought there may be a problem with the exit - it's there if not authorised but script moves to the main file if ok - does that make sense?
The authorisation part works fine, and like I said, with the other method, it displayed the first part of the echo ok, just not the user name.
Damn, I thought I was getting the hang of all this
Bookmarks