PHP中的null字符问题

由于 PHP 的文件系统操作是基于 C 语言的函数的,所以它可能会以您意想不到的方式处理 Null 字符。 Null字符在 C 语言中用于标识字符串结束,一个完整的字符串是从其开头到遇见 Null 字符为止。
借用官方手册中的一个例子:

$file = $_GET['file']; // "../../etc/passwd\0"
if (file_exists('/home/wwwrun/'.$file.'.php')) {
    // file_exists will return true as the file /home/wwwrun/../../etc/passwd exists
    include '/home/wwwrun/'.$file.'.php';
    // the file /etc/passwd will be included
}

以上代码在PHP5.3以前版本中,文件/etc/passwd将会被加载。对以后的版本没有影响。
虽然这个遇到\0会把字符截断的问题已经被修复了,但是在PHP中文件系统相关函数中还是有一点影响的,比如file_exists(),is_file()等。
如下代码:

$filename = "/etc/passwd\0"; 
$res = file_exists($filename); 
var_dump($res);

以上代码的执行结果如下:
Warning: file_exists() expects parameter 1 to be a valid path, string given in /usr/local/var/www/a.php on line 3
/usr/local/var/www/a.php:4:null

解决办法

对null字符进行替换

$input = str_replace(chr(0), '', $input);

96 Responses

  1. Just desire to say your article is as astounding. The clearness in your put up is simply spectacular and i can suppose you’re an expert on this
    subject. Well with your permission let me to grasp your RSS feed to stay updated with drawing close post.

    Thanks 1,000,000 and please continue the rewarding work.

  2. whoah this blog is fantastic i really like reading your posts.
    Stay up the good work! You recognize, a lot of people are looking round for this information, you could help them greatly.

  3. hello there and thank you for your information – I’ve definitely picked up anything
    new from right here. I did however expertise a few technical issues using this website,
    since I experienced to reload the site many times
    previous to I could get it to load correctly.
    I had been wondering if your hosting is OK? Not that I am complaining, but slow
    loading instances times will often affect your
    placement in google and could damage your high quality score
    if advertising and marketing with Adwords. Well I am adding
    this RSS to my email and could look out for much more of
    your respective fascinating content. Ensure that you update this again very soon.

  4. you’re really a excellent webmaster. The web site loading velocity is amazing.
    It seems that you’re doing any distinctive trick. Moreover, The contents
    are masterwork. you have done a wonderful process on this subject!

  5. Hi there, I found your web site by way of Google at the same time as looking for a comparable subject, your website
    got here up, it looks good. I have bookmarked it in my google bookmarks.

    Hello there, just become aware of your blog thru Google, and found that it’s truly informative.

    I am gonna watch out for brussels. I’ll be grateful in the event you proceed
    this in future. A lot of other folks will likely
    be benefited out of your writing. Cheers!

  6. Terrific article! That is the type of info that are meant to
    be shared across the net. Disgrace on Google for
    no longer positioning this put up upper! Come on over and seek advice
    from my web site . Thank you =)

  7. Someone necessarily help to make seriously articles I’d state.

    That is the very first time I frequented your web
    page and up to now? I surprised with the research you made to create this actual post extraordinary.

    Wonderful job!

  8. Heya i’m for the first time here. I found this board and I find It really useful & it helped me out
    a lot. I hope to give something back and aid others like you helped me.

  9. Hello there! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Many thanks

  10. Good day! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa?
    My website covers a lot of the same subjects as yours and I feel we could greatly benefit from each other.
    If you’re interested feel free to send me an e-mail. I look forward to hearing from you!
    Excellent blog by the way!

  11. Hello there! This is my first visit to your blog!
    We are a collection of volunteers and starting a new project in a
    community in the same niche. Your blog provided us useful information to work on. You have done a marvellous job!

  12. Hello there! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted
    keywords but I’m not seeing very good gains. If you know of any
    please share. Many thanks!

  13. Great article! That is the type of information that are supposed to
    be shared across the net. Disgrace on the seek engines for no longer positioning this
    submit higher! Come on over and visit my website .
    Thanks =)

  14. Hey there I am so excited I found your web site,
    I really found you by error, while I was researching on Aol for
    something else, Regardless I am here now and would just like to say
    cheers for a tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to go through it
    all at the moment but I have bookmarked it and also added in your RSS feeds, so
    when I have time I will be back to read a lot more, Please do keep up the superb
    work.

  15. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog?
    My website is in the exact same niche as yours and
    my visitors would certainly benefit from a lot of the information you present here.
    Please let me know if this alright with you. Thanks!

  16. You really make it appear so easy with your presentation but I find this topic to be actually something which I think I
    would by no means understand. It seems too complicated and very broad for me.

    I am looking forward to your next submit, I’ll try to get the hang of it!

  17. Hey there! I’ve been following your web site for a long time now and finally got the courage to go ahead and give you a shout out from Lubbock Texas!
    Just wanted to mention keep up the excellent job!

  18. Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

  19. I like the helpful info you provide to your articles.

    I’ll bookmark your weblog and test again right here regularly.
    I’m moderately certain I will be told many new stuff proper right here!
    Good luck for the following!

  20. Excellent post. I used to be checking continuously this weblog
    and I’m inspired! Extremely helpful information specifically the remaining section 🙂 I maintain such info a lot.
    I was seeking this certain info for a very lengthy time.
    Thank you and good luck.

  21. Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

  22. Hello very cool site!! Man .. Beautiful ..

    Wonderful .. I will bookmark your website and take the feeds also?
    I am happy to search out so many helpful info here in the
    put up, we’d like work out extra techniques on this regard, thanks for sharing.
    . . . . .

  23. Hi there! This post couldn’t be written much better!
    Looking through this article reminds me of my previous roommate!
    He constantly kept preaching about this. I’ll forward this post to him.
    Fairly certain he’ll have a very good read. Many thanks for sharing!

  24. I think this is among the most important information for
    me. And i am glad reading your article. But wanna remark on some general
    things, The web site style is wonderful, the articles is really excellent : D.
    Good job, cheers

  25. An impressive share! I have just forwarded this onto a colleague who has been conducting a little research on this.
    And he actually bought me lunch simply because I stumbled upon it for him…
    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending some time to talk about
    this issue here on your web page.

  26. Hello there, I discovered your blog via Google whilst searching for a similar matter, your site came up, it seems to be good.
    I’ve bookmarked it in my google bookmarks.
    Hi there, simply became alert to your weblog through Google, and located that it is really informative.
    I am going to be careful for brussels. I will appreciate for those who proceed this in future.
    A lot of folks will likely be benefited from your writing.

    Cheers!

  27. hello there and thank you for your information – I have certainly picked up anything new
    from right here. I did however expertise some
    technical points using this website, as I experienced to reload
    the website a lot of times previous to I could get it to load
    properly. I had been wondering if your web host is OK?
    Not that I am complaining, but sluggish
    loading instances times will very frequently affect your placement in google and could damage your high-quality score
    if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for
    much more of your respective fascinating content. Make sure
    you update this again soon.

  28. Its like you learn my mind! You seem to grasp a lot approximately
    this, like you wrote the e-book in it or something.
    I think that you just can do with some % to drive the message home a
    little bit, but instead of that, that is wonderful blog.

    An excellent read. I will certainly be back.

  29. I truly love your website.. Great colors & theme.

    Did you develop this website yourself? Please reply back as I’m trying to create my own personal website and want
    to learn where you got this from or just what the theme is named.

    Cheers!

  30. Hi there! I could have sworn I’ve been to your blog before but after looking at a few of the posts I realized it’s new to me.
    Regardless, I’m certainly pleased I came across it and
    I’ll be bookmarking it and checking back often!

  31. Today, I went to the beach with my kids. I found a sea shell
    and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell
    to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

  32. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet savvy so I’m not 100% sure.
    Any tips or advice would be greatly appreciated.
    Kudos

  33. Hey! I know this is kinda off topic however , I’d figured I’d ask.
    Would you be interested in trading links or maybe guest writing a
    blog post or vice-versa? My site addresses a lot of the same subjects as yours and I feel we could greatly benefit from each other.
    If you are interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Great blog by the way!

  34. Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is wonderful, let alone
    the content! natalielise pof

  35. I think this is one of the most vital information for me.
    And i am glad reading your article. But should remark on few general things,
    The web site style is perfect, the articles is really
    nice : D. Good job, cheers

  36. Hello, Neat post. There’s a problem along with your website in web explorer, could test this?
    IE nonetheless is the marketplace leader and a good part of people
    will miss your great writing because of this problem.

  37. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment
    is added I get several e-mails with the same comment.
    Is there any way you can remove people from that service? Cheers!

  38. Thanks for any other informative site. Where else may just I am getting that
    type of information written in such an ideal approach?
    I’ve a challenge that I’m just now working on, and I’ve been on the look out for such info.

  39. I feel that is among the so much significant information for me.
    And i am satisfied studying your article. However should observation on some common things, The site style is great, the articles is truly nice
    : D. Excellent task, cheers

  40. Definitely believe that which you said. Your favorite justification appeared to be on the net the simplest thing
    to be aware of. I say to you, I definitely get
    irked while people consider worries that they plainly do
    not know about. You managed to hit the nail upon the top and also defined
    out the whole thing without having side effect , people can take a
    signal. Will probably be back to get more. Thanks

  41. Terrific article! This is the type of information that are supposed to be shared around the net.
    Shame on Google for no longer positioning this publish
    upper! Come on over and consult with my web site .

    Thank you =)

发表评论

电子邮件地址不会被公开。 必填项已用*标注