Authorization with IIS
| From: | Danilo Meles | Date: | Sat, 05 Aug 2000 02:37:08 +0000 |
| Subject: | Authorization with IIS | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-1554@lists.php.net to get a copy of this message | ||
Hi There;
I'm trying to authenticate a user using the following code:
<? $auth = false; // Assume user is not authenticated
if (isset( $PHP_AUTH_USER ) && isset($PHP_AUTH_PW))
// Connect to MySQL
mysql_connect( 'host', 'user', 'pass' ) or die ( 'Unable to
connect to
server.' );
// Select database on MySQL server
mysql_select_db( 'db' ) or die ( 'Unable to select database.' );
// Formulate the query
$sql = "SELECT * FROM tableWHERE field = '$PHP_AUTH_USER' AND field=
'$PHP_AUTH_PW'";
// Execute the query and put results in $result
$result = mysql_query( $sql ) or die ( 'Unable to execute query.' );
// Get number of rows in $result.
$num = mysql_numrows( $result );
if ( $num != 0 )
// A matching row was found - the user is authenticated.
$auth = true; }
}
if ( ! $auth )
header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.'; exit; }
else { echo '<P>You are authorized!</P>'; } ?>
Putting It All Together - The Scripts Script for Authentication Using
Hard-Coded ValuesIf You Want to Print Out a Success Message:
<? if ( ( !isset( $PHP_AUTH_USER )) || (!isset($PHP_AUTH_PW)) ||
$PHP_AUTH_USER != 'user' ) || ( $PHP_AUTH_PW != 'open' ) )
header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.'; exit; }
else {echo 'Success!'; }
?>
If You Want To Relocate To a New Page or Print HTML:
<? // sample #1 redirect after success
if ( ( !isset( $PHP_AUTH_USER )) || (!isset($PHP_AUTH_PW)) ||
$PHP_AUTH_USER != 'user' ) || ( $PHP_AUTH_PW != 'open' ) )
header( 'WWW-Authenticate: Basic realm="Private"' );
eader( 'HTTP/1.0 401 Unauthorized' ); echo 'Authorization Required.';
exit; }
else
header( 'Location: http://www.server.com.br/' );
}
?>
I'm on Win2000 with Microsoft Internet Information Server (IIS) , but its
not working , could you help me???
Thank's you all
Danilo Meles