For a long time, as one of the biggest selling points of the server-side scripting language, PHP will actively create a global variable for the value submitted from the form. In PHP4.1, PHP producers recommended a data exchange method for visiting and submitting data. In PHP4
For a long time, as one of the biggest selling points of the server-side scripting language, PHP will actively create a global variable for the value submitted from the form. In PHP 4.1, PHP producers recommended a method for data exchange by visiting and submitting. In PHP 4.2, they canceled the old practice! As I will describe in this article, the goal of making such a change is to ensure security. We will study new PHP practices when processing form submissions and other data, and clarify why this improves code security.
What are the errors?
Take a look at the following PHP script, which is used to authorize access to a Web page when the input username and password are accurate:
// Check the user name and password
If ($ username = 'Kevin 'and $ password = 'secret ')
$ Authorized = true;
?>
Please enter your username and password:
Okay, I trust about half of the readers who will disdain to say "it's too clumsy-I won't make such a mistake !" However, I promise many readers will think, "Hi, there is no title, and I will write it like that !" Of course, a few people will be confused about the title ("What is PHP ?"). PHP is designed as a "good and easy" scripting language. beginners can learn to apply it within a short period of time. It should also be able to avoid beginners from making the above errors.
Return to the title. the title in the above code is that you can easily obtain the right to visit without providing accurate user names and passwords. Only add at the end of your browser's address bar? Authorized = 1. Because PHP will automatically create a variable for each submitted value-whether it is to take the initiative to submit a form, URL query string or a cookie-this will set $ authorized to 1, such an unauthorized user can break through the security restrictions.
So how can we solve this title briefly? You only need to set $ authorized to false by default at the beginning of the program. This title does not exist! $ Authorized is a variable completely created in the program code. But why do developers worry about the variable submitted by every malicious user?
What changes have PHP 4.2 made?
In PHP 4.2, the register_globals option in the newly installed PHP is disabled by default, therefore, EGPCS are not created as global variables (EGPCS are short for Environment, Get, Post, Cookies, and Server-this is the origin of all external variables in PHP. Of course, this option can also be enabled manually, but PHP developers recommend that you disable it. To fulfill their intentions, you need to apply other methods to obtain these values.
Starting from PHP 4.1, the EGPCS value can be obtained from a specified array: