The Header function and PHP_AUTH_USER perform user verification.

Source: Internet
Author: User
The Header function and PHP_AUTH_USER perform user verification. Php Header PHP_AUTH_USER PHP_AUTH_PW user authentication

In php, you can use the Header function to do some interesting things. user verification is one of the most interesting functions. Usage:

Header ("WWW-Authenticate: Basic realm =" user login "");

Header ("HTTP/1.0 401 Unauthorized ");

Design the two Header functions on the top page. a logon box is displayed before loading. you must enter the user name and password. When we get used to logging on to the page, do we think this login is very primitive and novel?

To obtain the username and password in the dialog box, you need to use the two special variables $ PHP_AUTH_USER and $ PHP_AUTH_PW provided by php. to use these two special variables, you need to use them in php. set related options in ini, otherwise it can only be referenced as below:

$ _ SERVER ['php _ AUTH_USER ']

$ _ SERVER ['php _ AUTH_PW ']

After obtaining the user name and password submitted by the user, the processing logic is no different from the 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 ";

?>

In order to enter this section of the web site, you must be an XXX

Subscriber. If you are a subscriber and you are having trouble logging

In,

Please contact support@xxx.com.

Exit;

} Else {

Mysql_pconnect ("localhost", "nobody", "") or die ("Unable to connect

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 ";

?>

In order to enter this section of the web site, you must be an XXX

Subscriber. If you are a subscriber and you are having trouble

Logging in,

Please contact support@xxx.com.

Exit;

}

$ Name = mysql_result ($ query, 0, "name ");

$ Email = mysql_result ($ query, 0, "email ");

Mysql_free_result ($ query );

}

?>

Source: http://www.weberdev.com/get_example-82.html

Another reference example:

// 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 is 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! ';

}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.