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>;
<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>;
<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;">$usr</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;">$pwd</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: #808080; font-style: italic;">// Does the user want to login or logout?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$usr</span> == <span style="color: #ff0000;">'jonas'</span> && <span style="color: #0000ff;">$pwd</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: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$usr</span> == <span style="color: #ff0000;">'reset'</span> && <span style="color: #0000ff;">$pwd</span> == <span style="color: #ff0000;">'reset'</span> && <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'Logout'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</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;">}</span>
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Logout</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// The user clicked on "Logout"</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;">'<br/>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<a href="http://'</span>.<span style="color: #0000ff;">$CurrentUrl</span>.<span style="color: #ff0000;">'">Login again</a>'</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</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 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!<br/>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<br/>'</span>;
<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 "real" login data with bogus data</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<a href="http://reset:reset@'</span>. <span style="color: #0000ff;">$CurrentUrl</span> .<span style="color: #ff0000;">'?Logout=1">Logout</a>'</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 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="Top-secret area"'</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>;
<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;">"Sorry, login failed!<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<br/>"</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<a href="http://'</span> . <span style="color: #0000ff;">$CurrentUrl</span> . <span style="color: #ff0000;">'">Try again</a>'</span>;
<span style="color: #66cc66;">}</span>
以上是关于php 带有注销功能的HTTP身份验证示例的主要内容,如果未能解决你的问题,请参考以下文章