Problems caused by register_globals in PHP

Source: Internet
Author: User

A few days ago. As a result of laziness, the app believes that building materials will use TG code again. The local test is complete and uploaded to the new virtual host. You cannot log on to phpinfo (). Check whether register_globals is enabled.
It is impossible to believe in the promise .. How can we effectively change the minimum amount of code so that all programs can run normally ..
When I was just learning PHP, in the demo in the official documentation, we recommend that you disable global variables and use $ _ request ["XXX"] and $ _ post ["XXX"] to obtain the data transmitted from the form, at that time, I did not go into the reason why I did this,
The force of inertia is really strong. Later, let's look at Myg (I just used the abbreviation, saving me some time and searching it by so361.com [Advertisement] or something .) After reading the program, I used global variables to pass the data submitted by the form. I didn't expect this to happen.
Let's go back and see how register_globals works .. What's wrong with opening up ..
In fact, for quite some time, one of the biggest selling points of PHP as the most widely used server-side scripting language is to automatically create a global variable for the value submitted from the form. This facilitates data sharing when multiple resources are redirected.
We usually write an authentication program like this:

Reference content is as follows:

$ Authorized = false; // a slightly careful programmer will initialize it to false
If (frm_username = "$ v_username" & $ frm_password! = "$ V_password ")
$ Authorized = ture;

If ($ authorized = false ){
; // The content displayed by the verified user here
} Else {
; // An unauthorized user will be prompted here

So, you only need to add? $ Authorized = false can successfully break through security restrictions and gain access power.
Therefore, after. *, the register_globals option in PHP. INI is disabled by default to prevent such incidents .. Therefore, the egpcs value is not created as a global variable. But they can be accessed through an array specified by their respective systems. They are called Super global variables: $ _ ENV, $ _ Get, $ _ post, $ _ cookie, and $ _ server. When are they used .. We can see from the name.
In fact, $ _ post is the most common one.
To run the above program .. We only need to add two lines of code in the above program:

Reference content is as follows:

$ Frm_username = $ _ POST ['frm _ username'];
$ Frm_password = $ _ POST ['frm _ password'];

Another advantage is that when register_globals is enabled previously, we need to add a line of code to get the values of $ frm_username and $ frm_password variables in the function.
Global $ frm_username, $ frm_password ,...;

Even if the $ HTTP_POST_VARS array is used to obtain the value passed by Form submission, we still need to import the array from the global scope:
Global $ HTTP_POST_VARS;
$ Frm_username = $ HTTP_POST_VARS ['frm _ username'];
$ Frm_password = $ HTTP_POST_VARS ['frm _ password'];

However, in PHP 4.1 and later versions, special $ _ POST variables (and other variables mentioned above) do not have to be so troublesome, the EGPCS value can be used in all ranges without being explicitly stated in the function,
In addition, the introduction of the special $ _ SESSION array greatly simplifies the session code. We do not need to declare the session variable as a global variable at all, and then pay attention to which variables are registered, now we can simply reference all SESSION variables from $ _ session ['varname.
The code to be modified is also very simple.

Reference content is as follows:

Session_start ();
If (frm_username = "$ v_username" & $ frm_password! = "$ V_password ")
$ _ SESSION ['authorized'] = true;

You only need to directly set the authorized variable in the $ _ SESSION array and use it in the same way. In this way, the program becomes shorter, and it will not cause confusion if there is a duplicate name in the global variable ..
It's that simple. Think about it. Two days ago, I tried to find a simple way to avoid modifying all the code. I just couldn't figure it out. Today I asked Meng Yuguo to submit a solution:

Reference content is as follows:

// Global variable settings
Function globals (){
Global $ HTTP_SESSION_VARS, $ HTTP_POST_VARS, $ HTTP_GET_VARS, $ HTTP_COOKIE_VARS;
While (list ($ tmp_key, $ tmp_value) = each ($ HTTP_SESSION_VARS )){
$ Name = $ tmp_key;
Global $ name;
$ Name = $ tmp_value;
}
While (list ($ tmp_key, $ tmp_value) = each ($ HTTP_POST_VARS )){
$ Name = $ tmp_key;
Global $ name;
$ Name = $ tmp_value;
}
While (list ($ tmp_key, $ tmp_value) = each ($ HTTP_GET_VARS )){
$ Name = $ tmp_key;
Global $ name;
$ Name = $ tmp_value;
}
While (list ($ tmp_key, $ tmp_value) = each ($ HTTP_COOKIE_VARS )){
$ Name = $ tmp_key;
Global $ name;
$ Name = $ tmp_value;
}
}

After getting it, you can write it on your own, and the code can be simpler:

Reference content is as follows:

If (! Empty ($ _ GET) extract ($ _ GET );
If (! Empty ($ _ POST) extract ($ _ POST );
If (! Empty ($ _ COOKIE) extract ($ _ GET );
If (! Empty ($ _ SESSION) extract ($ _ POST );

Check whether the variable is empty. If not empty, remove the array header and import it to the current symbol table.
Or use foreach to traverse the array and convert the format,

Reference content is as follows:

If (! Ini_get ('register _ globals '))
Foreach ($ _ POST as $ key => $ value)
Unset ($ GLOBALS [$ key]);
...
...

In the past few years, the idea of dependency is too heavy. If someone else has ready-made code, they will always use it if they want to get it.
Which of the following is why my mind is getting worse and worse? No wonder today, I am ridiculed that "I think you should not be a programmer ".

 

 

 

From: http://www.chinaz.com/Program/PHP/1104431b2008.html

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.