PHP新手上路(七)_php基础
建设一个简单交互的网站(三)
6. 密码验证
也许你想在自己的网站放上你的照片集,而且只想给自己知心的朋友看,这时你需要一个密码验证的程序。
6.1 基于HTTP验证
如何用PHP来实现密码验证的功能呢?我们可以使用简短的PHP代码,使用函数header()发送HTTP标头强制验证,客户端浏览器则弹出供输入用户名和密码的对话框。在PHP中,客户端用户输入的信息传送到服务端之后自动保存在$PHP_AUTH_USER, $PHP_AUTH_PW, 以及 $PHP_AUTH_TYPE这三个全局变量中。利用这些变量,我们就可以根据实现保存在数据文件或数据库中的用户帐号信息验证用户身份。
不过在这里需要提醒使用者注意的一点是:只有在Apache模块方式运行的时候,PHP脚本才能使用$PHP_AUTH_USER, $PHP_AUTH_PW, 以及 $PHP_AUTH_TYPE这三个变量。如果用户使用的是CGI模式的PHP则无法实现基于HTTP的验证功能。
6.2 下面,我们就来详细介绍一下如何使用PHP对用户身份进行验证。
在下例中,我们是使用$PHP_AUTH_USER和$PHP_AUTH_PW这两个变量来验证进入者是否合法并允许进入。在本例中被允许登录的用户名称和密码对分别为tnc和nature:
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 buttonn";
exit;
}
else
{
if ( !($PHP_AUTH_USER=="tnc" && $PHP_AUTH_PW=="nature") )
{
// 如果是错误的用户名称/密码对,强制再验证
Header("WWW-Authenticate: Basic realm="My Realm"");
Header("HTTP/1.0 401 Unauthorized");
echo "ERROR : $PHP_AUTH_USER/$PHP_AUTH_PW is invalid.";
exit;
}
else
{
echo "Welcome tnc!";
}
?>
事实上再实际引用中不大可能如上面使用代码段明显的用户名称/密码对,而是利用数据库或者加密的密码文件存取它们。
6.3 根据指定的验证信息核实用户身份
首先,我们可以使用以下代码确定用户是否已经输入了用户名和密码,并显示出用户输入的信息。
if (!isset($PHP_AUTH_USER)) {
header('WWW-Authenticate: Basic realm="My Private Stuff"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;
}
else {
echo "
You have entered this username: $PHP_AUTH_USER
You have entered this password: $PHP_AUTH_PW
The authorization type is: $PHP_AUTH_TYPE
}
?>
说明:
isset()函数用于确定某个变量是否已被赋值。根据变量值是否存在,返回true或false。
header()函数用于发送特定的HTTP标头。注意,使用header()函数时,一定要在任何产生实际输出的HTML或PHP代码前面调用该函数。
虽然上述代码相当简单,没有根据任何实际值对用户输入的用户名和密码进行有效验证,但是至少我们了解了如何使用PHP在客户端产生输入对话框。
下面,我们就来了解一下如何根据指定的验证信息核实用户身份。代码如下:
if (!isset($PHP_AUTH_USER)) {
header('WWW-Authenticate: Basic realm="My Private Stuff"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;
}
else if (isset($PHP_AUTH_USER)) {
if (($PHP_AUTH_USER != "admin") || ($PHP_AUTH_PW != "123")) {
header('WWW-Authenticate: Basic realm="My Private Stuff"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;
} else {
echo "
You're authorized!
";}
}
?>
在这里,我们首先检查用户是否已经输入了用户名称和密码,如果没有则弹出相应对话框要求用户输入身份信息。随后,我们通过判断用户输入的信息是否符合admin/123这一指定用户帐号来授予用户访问权限或提示用户再次输入正确的信息。这种方法适用于所有用户都使用同一登录帐号的网站。
6.4 另一种简易的密码验证
如果你是在windows98下面编写和运行着你的PHP脚本,或者是你在Linux下面按默认设置,将PHP安装成一个CGI程序的话,你将无法使用上面的PHP程序来实现验证功能。为此,无边给大家提供了另外一种简易的密码验证的方法。虽然实用性不大,但是拿来学习还是挺好的。
$password = "123";
// check password
if($pass != $password)
{
echo "
echo "";
}
else
{
echo "
echo "<script>"; <br>echo 'window.location="http://gophp.heha.net/test/index.php3"'; <br># 通过密码验证后转入的页面 <br>echo "</script>";
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
