<?php
/**
*/
if (
!isset ($_server[' Php_auth_user ')
|| !isset ($_server[' PHP_AUTH_PW ')
|| $_server[' Php_auth_user ']! = ' Qiku '
|| $_server[' PHP_AUTH_PW ']! = ' [email protected]$#@ '
) {
Header (' Www-authenticate:basic realm= ' Login ');
Header (' http/1.0 401 Unauthorized ');
Echo <<<eob
<big>wrong Username or password!</big>
</body>EOB;
Exit ();
}
?>
------------------------------------------------------------------------------------------------------
PHP Header php_auth_user PHP_AUTH_PW user authentication
In PHP, you can use the header function to do some interesting things, and user authentication is one of the most interesting features. Specific usage:
Header ("Www-authenticate:basic realm=" USER LOGIN ");
Header ("http/1.0 401 Unauthorized");
The two header functions are designed at the top of the page, and a login box appears before the page loads, asking for a user name and password. Used to login on the page, do we feel such a login is very primitive, and very novel?
In order to get the username and password from this dialog, we need to use two special variables $php_auth_user and $PHP_AUTH_PW provided by PHP, To use these two special variables in this way, it seems like you need to set the relevant options in php.ini, otherwise you can only refer to the following:
$_server[' Php_auth_user ']
$_server[' PHP_AUTH_PW ']
After obtaining the user name and password submitted by the user, how to handle the logic is no different from our General program processing. The following two routines are provided for reference:
<?
if (!isset ($PHP _auth_user)) {
Header ("Www-authenticate:basic realm=" XXX "");
Header ("http/1.0 401 Unauthorized");
$title = "Login instructions";
?>
<blockquote>
In order to enter this section of the Web site, you must is an XXX
Subscriber. If you is a subscriber and you is having trouble logging
In
Please contact <a href= "Mailto:[email protected]" >[email protected]</a>.
</blockquote>
<?
Exit
} else {
Mysql_pconnect ("localhost", "Nobody", "" ") or Die (" unable-to-connect to
SQL Server ");
mysql_select_db ("xxx") or Die ("Unable to select database");
$user _id=strtolower ($PHP _auth_user);
$password = $PHP _AUTH_PW;
$query = mysql_query ("SELECT * from Users where user_id= ' $user _id ' and
password= ' $password ');
if (!mysql_num_rows ($query)) {
Header ("Www-authenticate:basic realm=" XXX "");
Header ("http/1.0 401 Unauthorized");
$title = "Login instructions";
?>
<blockquote>
In order to enter this section of the Web site, you must is an XXX
Subscriber. If you is a subscriber and you is having trouble
Logging in,
Please contact <a href= "Mailto:[email protected]" >[email protected]</a>.
</blockquote>
<?
Exit
}
$name =mysql_result ($query, 0, "name");
$email =mysql_result ($query, 0, "email");
Mysql_free_result ($query);
}
?>
SOURCE page: http://www.weberdev.com/get_example-82.html
Another reference routine:
<?php
Assume user is not authenticated
$auth = false;
$user = $_server[' Php_auth_user ');
$pass = $_server[' PHP_AUTH_PW ');
if (Isset ($user) && isset ($pass))
{
Connect to DB
Include ' db_connect.php ';
SQL query to find if this entered Username/password was in the DB
$sql = "SELECT * FROM Healthed_workshop_admin WHERE
user = ' $PHP _auth_user ' and
pass = ' $PHP _auth_pw ' ";
Put the SQL command and SQL instructions into variable
$result = mysql_query ($sql) or Die (' Unable to connect. ');
Get number or rows in command; If more than 0, row is found
$num _matches = mysql_num_rows ($result);
if ($num _matches!=0)
{
Matching row found authenticates user
$auth = true;
}
}
if (! $auth)
{
Header (' Www-authenticate:basic realm= ' health Ed Presentation Admin ');
Header (' http/1.0 401 Unauthorized ');
Echo ' You must enter a valid username & password. ';
Exit
}
Else
{
Echo ' success! ';
}
?>
A simple PHP Verification login code