PHP encrypts text files and limits access to specific pages

Source: Internet
Author: User
I. INTRODUCTION although in general, your website is often quite simple and does not request application databases, in some cases, your website may want to restrict access to some pages. Generally, this means that you need to apply a database. I. INTRODUCTION

Although in general, your website is often quite simple, and it basically does not request an application database, in some cases, your website may want to restrict access to some pages. Generally, this means that you need to apply a database to store passwords and usernames. However, you have another easier method-although its security is a little poor, it only contains a very small amount of encoding.

If you have applied a database in your Web application, you can store passwords and usernames somewhere and use one way to authenticate visitors. However, what should I do if the application database cannot be guaranteed because of the security or complexity of your site? Sometimes you only want some special people to access some pages or areas of your site. Therefore, a simple method is to apply a text file that stores passwords and create a page to prompt visitors to enter passwords. if the password matches the content stored in the text file, otherwise, an appropriate message is displayed before the page is refreshed to prohibit access.

For further security, you can also use hash to encrypt passwords stored in text files. in this way, if the content is invented to some extent, it will also be difficult to find out. All of these can be built using the PHP method, and requires a very small amount of encoding.

Before the official start, you need to establish an environment to test and apply PHP. Therefore, you must first install and configure a Web server for PHP. Because Apache works well with PHP and is easy to install and configure, I recommend this program.

Next, you need to create a page (similar to)-It has a text box for receiving passwords from visitors, and a submit button to send it to your php file. This can be either a new page or a part of an existing page on your website. The simple code block below should be enough:

<Form name = 'passwordform' method = 'post' action = 'restricted. php'>
<P> Password:
<Input type = 'password' name = 'password'>
<Input type = 'submit 'name = 'submit 'value = 'login'>
</P>
</Form>

   2. create a PHP homepage

Next, you need to create a PHP homepage to complete the actual work. Open a vacant page in a text editor, and then open a PHP block in the scale method:

<?
As I mentioned earlier, PHP has a set of standard functions and methods for file control. The most important functions are fopen (), fread (), and fclose. To hold a file, we need to open it first, and obviously, this is implemented by applying the fopen () function. Moreover, we must specify how to hold the file; read the file, reading a file is the most common task, but some additional markup can be used to tell the program to put the file pointer at the beginning or the end of the file, and if the file does not exist, whether to create the file. However, in this example, we need to open a text file containing the password and read it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.