search
HomeBackend DevelopmentPHP Tutorial使用PHP3进行HTTP认证_PHP

使用PHP3进行HTTP认证_PHP

Jun 01, 2016 pm 12:27 PM
useCanpasswordWayuserCertificationidentityconductverify

只有在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\n";

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
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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version