Home php教程 php手册 使用PHP3进行HTTP认证

使用PHP3进行HTTP认证

Jun 13, 2016 am 10:26 AM
apache http php by use only Can exist Way module of Certification conduct

只有在PHP以Apache的模块方式运行的时候才可以使用HTTP认证的功能。在Apache的模块PHP脚本中,可以使用Header()函数向客户断浏览器发送一个”Authentication Required”的消息,使浏览器弹出一个用户名/密码(username/password)的输入窗口,当用户输入用户名和密码后,包含PHP脚本的URL将会被再次调用,使用分别代表用户名,密码,和确认方式的$PHP_AUTH_USER, $PHP_AUTH_PW,$PHP_AUTH_TYPE变量。现在只有”BASIC”的确认方式被支持。
在一个页面中强迫用户进行身份认证的代码段的例子如下:
Example 2-1. HTTP 认证举例:
if(!isset($PHP_AUTH_USER)) {
Header("WWW-Authenticate: Basic realm="My Realm"");
Header("HTTP/1.0 401 Unauthorized");
echo "Text to send if user hits Cancel button ";
exit;
}
else {
echo "Hello $PHP_AUTH_USER.
";
echo "You entered $PHP_AUTH_PW as your password.
";
}
?>
除了简单的输出$PHP_AUTH_USER 和 $PHP_AUTH_PW变量的值以外,你还可以检查用户名和密码的合法性,也许是对数据库进行查询,也许是在dbm文件中搜索用户。
当心臭虫成堆的Internet Explorer浏览器,他对Hearders的顺序非常挑剔.所以采用在送出HTTP/1.0 401 header 请求之前送出WWW-Authenticate header请求是一个很好的解决方法。


为了阻止一些人写一些脚本来显示一个经过传统外部机制验证过的页面的密码,采用如下方式:如果这一页面使用外部验证机制,将不会生成PHP_AUTH变量.这样,$REMOTE_USER变量可以被用来表示已经被外部机制验证的用户.
注意,上面的方法并不能防止某些人在同一台服务器上利用无身份验证的URL偷取有身份鉴别的URL的密码。
无论Netscape还是IE,在接到服务器的401回复之后,都将清空本地浏览器窗口的身份验证缓存。这种做法可以有效的使用户登录退出,从而强迫他们再次输入他们的用户名和密码。一些人使用这个方式来实现“超时”注册,或者提供登录退出的按钮。
这种方法不是标准的HTTP基本身份鉴定所必须的,所以您可能从来都不依靠它。使用Lynx进行的测试并没有弄清楚401服务器回应的身份鉴定,所以如果使用“向前”或“向后”功能将打开源文件(只要信用需求还没有被改变)。
虽然已经指出这种语言不能工作在Microsoft的IIS服务器上,但是PHP语言CGI版本将受到IIS的限制。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles