php 带有注销功能的HTTP身份验证示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 带有注销功能的HTTP身份验证示例相关的知识,希望对你有一定的参考价值。

<span style="color: #808080; font-style: italic;">// The full url to this file is required for </span>
<span style="color: #808080; font-style: italic;">// the Logout function</span>
<span style="color: #0000ff;">$CurrentUrl</span>         = <span style="color: #ff0000;">'www.jonasjohn.de/test_login.php'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Status flags:</span>
<span style="color: #0000ff;">$LoginSuccessful</span>    = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #0000ff;">$Logout</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;">$usr</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;">$pwd</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: #808080; font-style: italic;">// Does the user want to login or logout?</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usr</span> == <span style="color: #ff0000;">'jonas'</span> &amp;&amp; <span style="color: #0000ff;">$pwd</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: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usr</span> == <span style="color: #ff0000;">'reset'</span> &amp;&amp; <span style="color: #0000ff;">$pwd</span> == <span style="color: #ff0000;">'reset'</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;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Logout'</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> 
        <span style="color: #808080; font-style: italic;">// reset is a special login for logout ;-)</span>
        <span style="color: #0000ff;">$Logout</span> = <span style="color: #000000; font-weight: bold;">true</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Logout</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// The user clicked on &quot;Logout&quot;</span>
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'You are now logged out.'</span>;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;br/&gt;'</span>;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;a href=&quot;http://'</span>.<span style="color: #0000ff;">$CurrentUrl</span>.<span style="color: #ff0000;">'&quot;&gt;Login again&lt;/a&gt;'</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">else</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 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!&lt;br/&gt;'</span>;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;br/&gt;'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// This will not clear the authentication cache, but</span>
    <span style="color: #808080; font-style: italic;">// it will replace the &quot;real&quot; login data with bogus data</span>
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;a href=&quot;http://reset:reset@'</span>. <span style="color: #0000ff;">$CurrentUrl</span> .<span style="color: #ff0000;">'?Logout=1&quot;&gt;Logout&lt;/a&gt;'</span>;
<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 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;Top-secret area&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;
    <span style="color: #808080; font-style: italic;">// Error message</span>
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Sorry, login failed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span>;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;a href=&quot;http://'</span> . <span style="color: #0000ff;">$CurrentUrl</span> . <span style="color: #ff0000;">'&quot;&gt;Try again&lt;/a&gt;'</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>

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

Chrome 的 HTTP BASIC 身份验证注销问题

如何使用身份验证或注册、登录和注销来制作 React Native 和 Graphql 应用程序

Devise 的 HTTP 身份验证中断了常规注销

Chrome的HTTP BASIC身份验证注销问题

注销和记住会话的最佳 Worklight 实践

jquery注销功能