>  기사  >  백엔드 개발  >  PHP는 사용자 인증_php 팁을 위해 헤더 함수인 PHP_AUTH_PW 및 PHP_AUTH_USER를 사용합니다.

PHP는 사용자 인증_php 팁을 위해 헤더 함수인 PHP_AUTH_PW 및 PHP_AUTH_USER를 사용합니다.

WBOY
WBOY원래의
2016-05-16 19:53:131405검색

이 문서의 예에서는 PHP가 사용자 인증을 위해 헤더 함수인 PHP_AUTH_PW 및 PHP_AUTH_USER를 사용하는 방법을 설명합니다. 참고하실 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

PHP에서는 헤더 기능을 사용하여 몇 가지 흥미로운 작업을 수행할 수 있습니다. 사용자 확인은 흥미로운 기능 중 하나입니다. 구체적인 사용법:

Header("WWW-Authenticate: Basic realm="USER LOGIN"");
Header("HTTP/1.0 401 Unauthorized");

페이지 상단에 이 두 가지 헤더 기능을 디자인하세요. 페이지가 로드되기 전에 사용자 이름과 비밀번호가 필요한 로그인 상자가 나타납니다. 웹 로그인에 익숙한 우리들에게 이런 로그인이 참신하고 참신하다고 생각할까요?

이 대화 상자에서 전달된 사용자 이름과 비밀번호를 얻으려면 PHP에서 제공하는 두 개의 특수 변수 $PHP_AUTH_USER 및 $PHP_AUTH_PW를 이러한 방식으로 사용하려면 다음이 필요한 것 같습니다. php.ini 옵션에서 관련 설정을 지정하세요. 그렇지 않으면 다음과 같이만 인용할 수 있습니다:

$_SERVER['PHP_AUTH_USER']
$_SERVER['PHP_AUTH_PW']

사용자가 제출한 사용자 이름과 비밀번호를 얻은 후 로직을 처리하는 방법은 일반적인 프로그램 처리와 다르지 않습니다. 참조를 위해 아래에 두 가지 루틴이 제공됩니다.

<&#63;php
if(!isset($PHP_AUTH_USER)) {
Header("WWW-authenticate: basic realm="XXX"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
&#63;>
<blockquote>
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 <a href="mailto:support@xxx.com">support@xxx.com</a>.
</blockquote>
<&#63;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";
&#63;>
<blockquote>
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 <a href="mailto:support@xxx.com">support@xxx.com</a>.
</blockquote>
<&#63;php
exit;
}
$name=mysql_result($query,0,"name");
$email=mysql_result($query,0,"email");
mysql_free_result($query);
}
&#63;>

또 다른 참조 루틴:

<&#63;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 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!';
}
&#63;>

더 많은 PHP 관련 콘텐츠에 관심이 있는 독자는 이 사이트에서 "PHP 네트워크 프로그래밍 기술 요약", "PHP 기본 구문 튜토리얼 소개", "PHP 운영 오피스 문서 스킬 요약(워드, 엑셀, 액세스, ppt 포함)", "PHP 날짜 및 시간 사용 요약", "PHP 객체지향 프로그래밍 입문 튜토리얼》, "php 문자열(문자열) 사용법 요약", "php mysql 데이터베이스 작업 입문 튜토리얼" 및 "php 일반 데이터베이스 작업 스킬 요약"

이 기사가 PHP 프로그래밍에 종사하는 모든 사람에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.