PHP加密文本文件并限制特定页面的存取_PHP
一、 简介
尽管在一般情况下,你的网站往往相当简单以至它根本不要求使用数据库;但是在有些情况下,你的站点可能想限制对某些页面的存取。一般地,这意味着要使用一个数据库来存储口令和用户名。然而,你还有一个更容易的方法-尽管其安全性差了一点,但是它仅包含了极少的编码。
如果你在自己的Web应用程序中使用了数据库,那么你已经能够在某处存储口令和用户名,并且有一种方法可用来认证访问者。但是,当因你的站点的安全性或复杂性需要而无法保证使用数据库时,那该怎么办?可能有些时候你仅想要一些特别的人存取你的站点的某些页面或一些区域。为此,一种很简单的方式就是使用一个存储口令的文本文件,并且创建一个页面来提示访问者输入口令;如果口令与存储在该文本文件中的内容相匹配,那么允许该用户存取受限的页面;否则,在开始刷新页面之前显示一条适当的消息以禁止其存取。
为了进一步的安全起见,你还可以用哈希法来加密存储在文本文件中的口令,这样如果它的内容在某种程度上被发现,它还将会是很难被查明。所有这些都可以用PHP方法来构建,而且只需极少量的编码。
在正式开始前,你需要建立一个环境来测试和使用PHP;所以,你首先需要为PHP安装和配置一个Web服务器。由于Apache与PHP协同工作良好,并且易于安装和配置,因此我推荐使用这一方案。
接下来,你需要创建一个页面(类似下图)-它有一个文本框用于接收来自访问者的口令,还有一个submit按钮来把它发送到你的PHP文件。这既可以是一个新页面也可以是你的网站上的一个现有页面的一部分。象下面简单的代码块应该足够了:
<form name="passwordForm" method="post" action="restricted.php">
<p>Password:
<input type="password" name="password">
<input type="submit" name="Submit" value="Login">
</p>
</form>
二、 创建PHP主页
接下来,你需要创建完成实际工作的PHP主页。在一个文本编辑器中打开一个空白页面,然后以标准方式打开一个PHP块:
<?
如我以前所提及的,PHP具有一套标准的函数和方法用来实现文件操作。其中,最为主要的几个是fopen(),fread()和fclose()函数。为了进行某种文件操作,我们需要先打开它,并且很明显,这是使用fopen()函数实现的;而且,我们必须指定如何操作文件;读文件,读文件是最普通的任务,但是另外一些额外标志可以用来告诉程序是把文件指针放在开始还是文件的结束,以及如果文件还不存在的话是否创建该文件。然而,在本例中,我们所需要做的是,打开包含口令字的文本文件并读取它。
然后,首先创建一个到指定文本文件路径的变量:
$fileloc = "/apachesite/docs/pass.txt"
接下来,创建一个变量来存放文件指针:
$filetoread = fopen($fileloc, "r") or die("Could not open password file");
你也可以使用die方法来结束该脚本,并且如果操作因某种原因失败的话将在屏幕上打印一条适当的消息。一旦打开文件,你需要读取它的内容,以便把它与以口令字形式输入的内容进行比较:
$storedpass = fread($filetoread, filesize($fileloc)) or die ("Could not read stored password");
你应该设置一个变量来存放文件中的数据,并且调用fread()方法(它有两个参数:文件指针和文件长度)。你可能知道(也可能不知道)你的口令的长度。为了使将来的编程容易些(当口令字需要改变时),你可以使用filesize()方法来得到文件长度。一旦文件不再需要,立即关闭之:
fclose($filetoread);
三、 使用口令
为了使用输入到HTML表单中的口令,你需要得到它并把它存储到一个变量中。当我们使用POST方法来把用户输入的内容发送到PHP脚本时,我们可以使用$_POST来取得输入的口令:
$password = $_POST["password"];
然后,我们可以简单地把输入的口令字与存储的口令字进行比较并采取相应的措施:
if (empty ($password)){
die ("No password entered");
}
elseif ($password != $storedpass){
die ("Password Incorrect");
}
else{
Header("Location: securepage.htm")
}
?>
第一个if语句处理一个空的$password变量以防止当输入框为空时,submit按钮被点击。如果用户输入的口令与存储的那个不匹配,那么第二个语句执行括号内的代码并且输出一条消息显示口令是错误的。最后,如果前两个条件都不满足,那么,该脚本认为口令一定是正确的并且把一个重定向头(header)发送到浏览器以打开示例中的HTML页面。
在此能够工作之前,你需要创建一个文本文件并且把它放到与该PHP文件相同的目录下。它需要包含你目前想要使用的以普通文本形式存储的口令,并且应该引用该PHP文件名。保存所有这些文件,然后在一个浏览器中打开该HTML页面,并用该表单进行试验。该页面应该如所设想的那样工作。
当你输入正确的口令时,如果你得到一个错误消息,其内容是:
"Warning: Cannot modify header information - headers already sent by (thepathtoyourphpfile)"
这意味着,你需要把位于你的Windows目录下的php.ini文件中的output-buffering设置为"on"。
四、 加密
现在,我们开始分析在前面提到的加密问题。PHP具有一些内置的MD5方法。这样以来,在把访问者输入的口令与存储的口令进行比较之前,我们可以很容易地使用这些函数来转换它。
MD5是一种单向哈希算法,这意味着口令可以仅用一种方向进行加密-从普通文本到加密文本,而以另外一种方向是不可能的。然而,这并不是就能使得它不可破解。这种加密容易被以暴力方式或者通过字典攻击加以破解,但是它仍然还是比较安全的。你可以把下列一行添加到$password变量的声明语句之后:
$md5password = (md5($password));
这样可以把一个输入到该文本框中的内容的加密版本保存到变量$md5password中。现在,你需要修改你的if语句,以便它把存储的口令与新的加密的口令加以比较:
if (empty ($password))
{
die ("No password entered");
}
elseif ($md5password != $storedpass)
{
die ("Password Incorrect");
}
else
{
header("Location: securepage.htm");
}
如你所见,我们仅改变了语句的elseif部分中的变量。这是因为即使是一个空的输入变量也被哈希化为一个32位值,因此$md5variable永远不可能为空,-即使在把任何文本输入到输入域之前点击submit按钮。
现在,所有你需要做的就是,找到你想存储在文本文件pass.txt中的该口令的哈希值。为此,你可以注释掉整个的if语句并且再加上一个echo语句以把加密的口令显示在屏幕上。然后,你还可以复制加密串并且把它保存到口令文件中。然而,你必须记住,在使用该脚本之前,取消注释if语句并且删除echo调用。
就本文所讨论的方法而言,上面的脚本框架所提供的已经足够了。另外,本文所讨论的测试文件,尽管非常基本,但是该HTML页面能够被容易地加入到一个现有的页面中去;你可以把它粘贴到一个窗口中去并整理它的样式以匹配你的主页的其余部分,并且你可能包含一个定时函数-它在把访问者重新定向到一个安全的页面前等待一段固定的时间,同时显示一条消息指示口令正确。你还可以包含一个类似的函数集来重载初始页面。
总之,你可以使用本文所提供的脚本来限制到你的站点结构中的特定页面的存取。尽管该方法并没有提供一个数据库所提供的安全的用户名/口令认证方法,并且它意味着你必须把口令发给想存取安全页面的任何人,但是它的确耗费非常少的时间和编码提供了一种简单的安全层。

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

AI Hentai Generator
Generate AI Hentai for free.

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



Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

With the popularity of video accounts on social media, more and more people are beginning to use video accounts to share their daily lives, insights and stories. However, some users may experience comments being restricted, which can leave them confused and dissatisfied. 1. How to remove comment restrictions on video accounts? To lift the restriction on commenting on a video account, you must first ensure that the account has been properly registered and real-name authentication has been completed. Video accounts have requirements for comments. Only accounts that have completed real-name authentication can lift comment restrictions. If there are any abnormalities in the account, these issues need to be resolved before comment restrictions can be lifted. 2. Comply with the community standards of the video account. Video accounts have certain standards for comment content. If the comment involves illegal content, you will be restricted from speaking. To lift comment restrictions, you need to abide by the community of the video account

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension ".a" have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run ".a" files. This article will introduce how to comprehensively install and configure the Linux ".a" file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux ".a" file. What is L

Title: Introduction to how to delete a page of content in Word When editing a document using Microsoft Word, you may sometimes encounter a situation where you need to delete the content of a certain page. You may want to delete a blank page or unnecessary content on a certain page in the document. In response to this situation, we can take some methods to quickly and effectively delete a page of content. Next, some methods to delete a page of content in Microsoft Word will be introduced. Method 1: Delete a page of content First, open the Word document that needs to be edited. Certainly
