Php security hazards caused by setting registerglobals to TRUE

Source: Internet
Author: User
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:

  1. $ GLOBALS ['host'] = 'localhost ';
  2. $ GLOBALS ['username'] = 'root ';
  3. $ GLOBALS ['password'] = '';
  4. $ GLOBALS ['database'] = 'test ';
  5. ?>

The following code is available in db. php:

  1. Require_once 'config. php ';
  2. Function db_connect (){
  3. $ Db = mysql_connect ($ GLOBALS ['host'], $ GLOBALS ['username'], $ GLOBALS ['password']);
  4. Mysql_select_db ($ GLOBALS ['database'], $ db );
  5. Return $ db;
  6. }
  7. ?>

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.

  1. Function wp_unregister_GLOBALS (){
  2. If (! Ini_get ('register _ globals '))
  3. Return;
  4. If (isset ($ _ REQUEST ['globals'])
  5. Die ('globals overwrite attempt detected ');
  6. // Variables that shouldn't be unset
  7. $ NoUnset = array ('globals', '_ get',' _ post', '_ cookies',' _ request', '_ server',' _ env ', '_ Files ');
  8. $ Input = array_merge ($ _ GET, $ _ POST, $ _ COOKIE, $ _ SERVER, $ _ ENV, $ _ FILES, isset ($ _ SESSION) & is_array ($ _ SESSION )? $ _ SESSION: array ());
  9. Foreach ($ input as $ k => $ v)
  10. If (! In_array ($ k, $ noUnset) & isset ($ GLOBALS [$ k]) {
  11. $ GLOBALS [$ k] = NULL;
  12. Unset ($ GLOBALS [$ k]);
  13. }
  14. }

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.