php 简单的HTTP身份验证示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单的HTTP身份验证示例相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">// Status flag:</span>
<span style="color: #0000ff;">$LoginSuccessful</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #808080; font-style: italic;">// Check username and password:</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_USER'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> && <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_PW'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$Username</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_USER'</span><span style="color: #66cc66;">]</span>;
<span style="color: #0000ff;">$Password</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_PW'</span><span style="color: #66cc66;">]</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Username</span> == <span style="color: #ff0000;">'jonas'</span> && <span style="color: #0000ff;">$Password</span> == <span style="color: #ff0000;">'foobar'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$LoginSuccessful</span> = <span style="color: #000000; font-weight: bold;">true</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// Login passed successful?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$LoginSuccessful</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">/*
** The user gets here if:
**
** 1. The user entered incorrect login data (three times)
** --> User will see the error message from below
**
** 2. Or the user requested the page for the first time
** --> Then the 401 headers apply and the "login box" will
** be shown
*/</span>
<span style="color: #808080; font-style: italic;">// The text inside the realm section will be visible for the </span>
<span style="color: #808080; font-style: italic;">// user in the login box</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'WWW-Authenticate: Basic realm="Secret page"'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'HTTP/1.0 401 Unauthorized'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"Login failed!<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// The user entered the correct login data, put</span>
<span style="color: #808080; font-style: italic;">// your confidential data in here: </span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'you reached the secret page!'</span>;
<span style="color: #66cc66;">}</span>
以上是关于php 简单的HTTP身份验证示例的主要内容,如果未能解决你的问题,请参考以下文章