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>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Check username and password:</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'PHP_AUTH_USER'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'PHP_AUTH_PW'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0000ff;">$Username</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'PHP_AUTH_USER'</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #0000ff;">$Password</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'PHP_AUTH_PW'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Username</span> == <span style="color: #ff0000;">'jonas'</span> &amp;&amp; <span style="color: #0000ff;">$Password</span> == <span style="color: #ff0000;">'foobar'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$LoginSuccessful</span> = <span style="color: #000000; font-weight: bold;">true</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Login passed successful?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$LoginSuccessful</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* 
    ** The user gets here if:
    ** 
    ** 1. The user entered incorrect login data (three times)
    **     --&gt; User will see the error message from below
    **
    ** 2. Or the user requested the page for the first time
    **     --&gt; Then the 401 headers apply and the &quot;login box&quot; will
    **         be shown
    */</span>
&nbsp;
    <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;">&#40;</span><span style="color: #ff0000;">'WWW-Authenticate: Basic realm=&quot;Secret page&quot;'</span><span style="color: #66cc66;">&#41;</span>;
    <a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'HTTP/1.0 401 Unauthorized'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Login failed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <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>
&nbsp;
    <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;">&#125;</span>

以上是关于php 简单的HTTP身份验证示例的主要内容,如果未能解决你的问题,请参考以下文章

PHP 简单的PHP身份验证HTTP

简单PHP身份验证HTTP

如何在 PHP 中使用基本 HTTP 身份验证?

php PHP基本身份验证示例

Python 中的 API 调用身份验证(工作 PHP 示例)

在 PHP 中访问 HTTP 身份验证用户名?