Many people have seen the danger of setting registerglobals to TRUE. but how can this problem be solved? A lot of new users should not know! So let me briefly talk about the dangers of setting registerglobals to on!
Many people have seen the danger of setting register globals to TRUE. but how can this problem be solved? A lot of new users should not know! So let me briefly talk about the dangers of setting register globals to on!
Let's take a look at the following code:The following code is available in the config. php file:
-
- $ GLOBALS ['host'] = 'localhost ';
- $ GLOBALS ['username'] = 'root ';
- $ GLOBALS ['password'] = '';
- $ GLOBALS ['database'] = 'test ';
- ?>
The following code is available in db. php:
-
- Require_once 'config. php ';
- Function db_connect (){
- $ Db = mysql_connect ($ GLOBALS ['host'], $ GLOBALS ['username'], $ GLOBALS ['password']);
- Mysql_select_db ($ GLOBALS ['database'], $ db );
- Return $ db;
- }
- ?>
Obviously, the above code is added when register globals is set to on to see what the effect is:
In the browser, enter http: // ********/index. php? GLOBALS = *** then the code above will be executed incorrectly! Because $ GLOBALS data has been modified! When register globals is set to on ,? GLOBALS = *** is converted to $ GLOBALS = !! So it is very dangerous.
Although this example is not harmful, we should check whether register GLOBALS is set to on in some places where the $ globals global variable is needed. you can use the following code to check whether the register GLOBALS is set to on.
- Function wp_unregister_GLOBALS (){
- If (! Ini_get ('register _ globals '))
- Return;
- If (isset ($ _ REQUEST ['globals'])
- Die ('globals overwrite attempt detected ');
- // Variables that shouldn't be unset
- $ NoUnset = array ('globals', '_ get',' _ post', '_ cookies',' _ request', '_ server',' _ env ', '_ Files ');
- $ Input = array_merge ($ _ GET, $ _ POST, $ _ COOKIE, $ _ SERVER, $ _ ENV, $ _ FILES, isset ($ _ SESSION) & is_array ($ _ SESSION )? $ _ SESSION: array ());
- Foreach ($ input as $ k => $ v)
- If (! In_array ($ k, $ noUnset) & isset ($ GLOBALS [$ k]) {
- $ GLOBALS [$ k] = NULL;
- Unset ($ GLOBALS [$ k]);
- }
- }