PHP-protected file system specific code sharing _php tutorial

Source: Internet
Author: User
File systems are very important to any site, and programmers are sparing no effort to protect their systems from being violated. Today we will explain the specific code examples of the PHP protection file system. There was a Web site that disclosed customer data that was stored in a file in the Web server. A visitor to the Web site uses the URL to view the files that contain the data. Although the file was misplaced, this example highlights the importance of protecting the file system against attackers.

  • The application arbitrarily processes the file and contains variable data that the user can enter, and carefully examines the user input to ensure that the user cannot perform any inappropriate actions on the file system. Listing 1 shows an example of a PHP site that downloads an image with the specified name.

       
       
    1. Php
    2. if ($_post[' submit '] = = ' Download ') {
    3. $ file = $_post[' fileName '];
    4. Header ("Content-type:application/x-octet-stream");
    5. Header ("Content-transfer-encoding:binary");
    6. header ("Content-disposition:attachment; filename = "". $file. "";" );
    7. $ FH = fopen ($file, ' R ');
    8. while (! feof ($FH))
    9. {
    10. Echo (Fread ($FH, 1024));
    11. }
    12. Fclose ($FH);
    13. } else {
    14. Echo ("<html><head>< ");
    15. Echo ("title>Guard your filesystem title> c19> head> ");
    16. Echo ("<body><form ID = "Myfrom" Action = "". $_server[' php_self '.
    17. "" Method = "POST" > ");
    18. Echo ("<Div><input type= "text" name= "FileName" value= "");
    19. Echo (isset ($_request[' filename '])? $_request[' filename ']: ');
    20. Echo ( " />");
    21. Echo ("<input type=" Submit " value = "Download" name= "Submit" /> Div > ");
    22. Echo (" form> body> html > ");
    23. }

    As you can see, the more dangerous script in Listing 1 will handle all the files that the WEB server has read access to, including the files in the Session directory (see "Securing Session Data"), and even some system files (such as/etc/passwd). For a php-protected file system demonstration, this example uses a text box that allows the user to type a file name, but can easily provide a file name in the query string.

    While configuring user input and file system access is risky, it is a good idea to design your application to use the database and hide the generated file names to avoid simultaneous configuration. However, this does not always work. Listing 2 provides a sample routine to verify the file name. It will use regular expressions to ensure that only valid characters are used in the file name, and specifically check for dot characters: ...

     
        
        
    1. function Isvalidfilename ($file) {
    2. /* don ' t allow. and allow any "word" character /* *
    3. Return Preg_match ('/^ (?:.) (?!.))| W) +$/', $file);

    The above is the article for everyone to share the PHP protection file system specific code to write.


    http://www.bkjia.com/PHPjc/446399.html www.bkjia.com true http://www.bkjia.com/PHPjc/446399.html techarticle file systems are very important to any site, and programmers are sparing no effort to protect their systems from being violated. Today, we will explain the PHP protection text for you ...

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.