This article mainly introduces how php uses the Header function, PHP_AUTH_PW and PHP_AUTH_USER for user authentication, combined with the instance form, this article analyzes the PHP login verification using the Header function call and the skills related to PHP_AUTH_PW and PHP_AUTH_USER for verification. if you need it, refer to the example below to describe how php uses the Header function, PHP_AUTH_PW and PHP_AUTH_USER are used for user authentication. We will share this with you for your reference. The details are as follows:
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:
<?phpif(!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 XXXsubscriber. If you are a subscriber and you are having trouble loggingin,please contact support@xxx.com.
<?phpexit;} 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";?>
In order to enter this section of the web site, you must be an XXXsubscriber. If you are a subscriber and you are having troublelogging in,please contact support@xxx.com.
<?phpexit;}$name=mysql_result($query,0,"name");$email=mysql_result($query,0,"email");mysql_free_result($query);}?>
Another reference example:
<?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 dbinclude 'db_connect.php';//SQL query to find if this entered username/password is in the db$sql = "SELECT * FROM healthed_workshop_admin WHEREuser = '$PHP_AUTH_USER' ANDpass = '$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!';}?>