This article illustrates how PHP uses the header function, PHP_AUTH_PW and php_auth_user to do user authentication. Share to everyone for your reference, specific as follows:
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 is loaded, asking for a username and password. Accustomed to the page to log in, whether we feel that such a login is very original, and very novel it?
To get the username and password from this dialog box, you need to use the two special variables $php_auth_user and $PHP_AUTH_PW that PHP provides. Using these two special variables like this requires that you set the relevant options in php.ini, or you can only refer to the following as follows:
$_server[' Php_auth_user ']
$_server[' PHP_AUTH_PW ']
After obtaining the user name and password submitted by the user, how to deal with the logic is no different from our normal program processing. The following two routines are available for reference:
<?php 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, your must be a XXX s Ubscriber. If you are are a subscriber and you are have trouble logging in, please contact <a href= "mailto:support@xxx.com" >suppo
Rt@xxx.com</a>.
</blockquote> <?php 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, your must be a XXX s Ubscriber. If you are A subscriber and you are have trouble logging in, please contact <a href= "mailto:support@xxx.com" >support@xxx.com
</a>.
</blockquote> <?php exit;
$name =mysql_result ($query, 0, "name");
$email =mysql_result ($query, 0, "email");
Mysql_free_result ($query);
}?>
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 are in the db
$sql = ' select * from Healthed_workshop_admin wher E
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, the 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! ';
}
? >
More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Introduction to object-oriented Programming, PHP String (String) Usage summary, PHP+MYSQL database Operations Tutorial and PHP Common database operating Skills summary
I hope this article will help you with the PHP program design.