PHP Security Basics Chapter 1 Introduction to PhP Functions

Source: Internet
Author: User
I. php functions:
PHP has many functions suitable for web development. Some common jobs that are hard to implement in other languages become easy in PHP, which has both advantages and disadvantages. Register_globals is a feature that is more eye-catching than other features.

1.1.1. Register global variables

If you still remember to use C to develop CGI in early web application developmentProgramYou will have a deep understanding of complicated form processing. When the register_globals configuration option of PHP is enabled, complicated original form processing no longer exists, and public variables are automatically created. It makes PHP programming easy and convenient, but it also brings security risks.

In fact, register_globals is innocent, and it does not produce any vulnerabilities, and developers must make mistakes. However, you have to disable register_globals when developing and deploying applications for two main reasons:

First, it will increase the number of security vulnerabilities;

Second, it hides the data source and violates the developer's responsibility to track data at any time.

All examples in this book assume that register_globals has been disabled and replaced with super public arrays such as $ _ Get and $ _ post. Using these arrays is almost as convenient as the programming method when register_globals is enabled, and some of them are not worth it because it improves program security.

Tips

If you must develop an application deployed in the environment enabled by register_globals, it is important that you initialize all variables and set error_reporting to e_all (or e_all | e_strict) to warn of uninitialized variables. When register_globals is enabled, any use of uninitialized variables almost means a security vulnerability.

1.1.2. Error Report

 

No developer will make mistakes. php's error reporting function will help you confirm and locate these errors. These detailed descriptions provided by PHP may also be seen by malicious attackers. It is important that the public cannot see the error message. To do this, you only need to disable display_errors. If you want error information, you can enable the log_errors option and set the saving path of the error log file in the error_log option.

Some errors cannot be found due to the level setting of the error report. You must set error_reporting to e_all (e_all | e_strict is the highest setting and provide downward compatible suggestions, if not recommended ).

All error reporting levels can be modified at any level. Therefore, if you are using a shared host, you are not authorized to access PHP. INI, httpd. conf, or. when changing configuration files such as htaccess, you can run the Error Report-level Configuration statement in the program:

 

Code:

 

<? PHP

 

Ini_set ('error _ report', e_all | e_strict );

Ini_set ('display _ errors ', 'off ');

Ini_set ('Log _ errors ', 'on ');

Ini_set ('error _ log', '/usr/local/Apache/logs/error_log ');

 

?>

 

Tips

The http://php.net/manual/ini.php gives a detailed description of the Option configuration of PHP. ini.

 

PHP also allows you to use the set_error_handler () function to specify your own error handling function:

 

Code:

 

<? PHP

 

Set_error_handler ('My _ error_handler ');

 

?>

 

The above Program specifies your own error handling function my_error_handler (); the following is an actual example:

 

Code:

 

<? PHP

 

Function my_error_handler ($ number, $ string, $ file, $ line, $ context)

{

$ Error = "=========\ NPHP error \ n ===========\ N ";

$ Error. = "number: [$ number] \ n ";

$ Error. = "string: [$ string] \ n ";

$ Error. = "file: [$ file] \ n ";

$ Error. = "line: [$ line] \ n ";

$ Error. = "context: \ n". print_r ($ context, true). "\ n ";

 

Error_log ($ error, 3, '/usr/local/Apache/logs/error_log ');

}

 

?>

 

Tips

PHP 5 also allows the second parameter to be passed to set_error_handler () to limit the execution of the defined error handling function in case of any error. For example, you can create a function to handle Warnings:

 

Code:

 

<? PHP

Set_error_handler ('My _ warning_handler ', e_warning );

?>

 

PhP5 also provides an exception handling mechanism, see http://php.net/exceptions

 

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.