Knowledge of security precautions in PHP programming

Source: Internet
Author: User
Tags form post
    1. Register_global = Off
    2. MAGIC_QUOTES_GPC = Off
    3. Display_error = Off
    4. Log_error = On
    5. # allow_url_fopen = Off
    6. expose_php = Off
    7. Open_basedir =
    8. Safe_mode = On
    9. Disable_function = Exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen, Show_source,get_cfg_var
    10. Safe_mode_include_dir =
Copy Code

2,db SQL preprocessing mysql_real_escape_string (many phper still rely on addslashes to prevent SQL injection, but this approach still has a problem with Chinese encoding.) Addslashes the problem is that hackers can use 0xbf27 instead of single quotes, GBK encoding 0xbf27 is not a legal character, so addslashes just 0xbf5c27 to become a valid multibyte character, 0XBF5C will still be considered as single quotes). With the mysql_real_escape_string function you also need to specify the correct character set, otherwise there may be a problem.

Prepare + Execute (PDO) zendframework can be used in DB class quote or Quoteinto, these two methods are based on a variety of databases implemented without methods, not like Mysql_real_escape_ String can only be used for MySQL.

3, user input processing without preserving HTML tags can be strip_tags in the following ways, delete all HTML tags in string htmlspecialchars, only for "<", ">", ";", "'" Characters are escaped htmlentities, the following tools can be considered in cases where HTML tags must be preserved for all HTML to be escaped:

    1. HTML purifier:html Purifier is a standards-compliant the HTML filter library written in PHP.
    2. PHP HTML sanitizer:remove Unsafe tags and attributes from HTML code
    3. htmlawed:php code to purify & filter HTML
Copy Code

4, upload file with Is_uploaded_file and move_uploaded_file function, use http_post_files[] array. And to prevent users from uploading PHP scripts by removing the PHP interpretation function of the upload directory. ZF framework can consider using the File_upload module Session,cookie and form security processing do not rely on cookies for core authentication, important information needs to be encrypted, Form post before the transfer of data hash. For example, the form element to be emitted is as follows:

    1. Verify the parameters after the post is back
    2. $str = "";
    3. foreach ($_post[' H '] as $key = = $value) {
    4. $str. = $key. $value;
    5. }
    6. if ($_post[' hash ']! = MD5 ($str. $secret)) {
    7. echo "Hidden form data Modified"; Exit
    8. }
Copy Code

5,php Security Detection Tool (XSS and SQL insertion) Wapiti-web Application Security Auditor (Wapiti-Small Site Vulnerability Detection Tool) (SQL INJECTION/XSS attack checker)

6, Installation/use:

    1. Apt-get Install libtidy-0.99-0 python-ctypes python-utidylib
    2. Python wapiti.py http://Your Website url/-M GET_XSS
Copy Code

PIXY:XSS and SQLI Scanner for PHP (pixy-php source Defect analysis tool) installed: Apt-get Install DEFAULT-JDK

Just introduce these, if you can do all of the security measures described above, your PHP code is very safe.

  • 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.