PHP - Is “include” function secure?

前端 未结 8 852
傲寒
傲寒 2020-12-06 06:28

I\'m using the \"include\" function (e.x. \"include \'header2.php\'\" or \"include \'class.users.php\'\") to add the header or session class in my website. I don\'t really r

8条回答
  •  爱一瞬间的悲伤
    2020-12-06 07:30

    Include is safe provided you don't:

    1. Include a remote file like www.someoneelsesssite.com/something.php
    2. Include a file from a path that came from the client. www.mysite.com/bad.php?path=oops/here/is/your/passwords/file
    3. Include a file from another possibly tainted source like a database.

    2 and 3 technically have the caveat that if you disallow . or / or on windows \ you are probably fine. But if you don't know why, you don't know enough about it to risk it. Even when you think the database is read only or otherwise secure, it is wise to not assume that unless you really have to, which is almost never.

    As pp19dd's answer points out. It is also vital that you name your includes with the .php extension. If you've set apache (or whatever web server you are using) to parse another file type as PHP too, that's safe as well. But if you don't know for sure, use .php exclusively.

提交回复
热议问题