LSX-blog

Feed Rss

【收录】PHP中stripslashes和addslashes的使用

11.24.2011, Linux, by .

向mysql写入数据时,
如:mysql_query(“update tableName set `title`=’LSX’s blog'”);

这个时候,PHP将会报错,因为数据库对单引号过敏。

ASP中需要进行replace(“‘”,”””,str);

PHP中则可以直接使用addslashes。

ASP问题这里暂不考虑,本文要说的是PHP相关的stripslashes和addslashes。

PHP考虑到其安全性,引入了magic_quotes_gpc = On/Off 这个参数,可以在不做任何处理的情况下就直接将含有单引号的数据插入数据库中。如果magic_quotes_gpc = Off,需要考虑单引号的问题了,不能一味地信任其运行环境。

当magic_quotes_gpc = On时,使用addslashes()处理后的数据在数据库中将以\’形式保存,如果此时直接输出的话,就会发现比自己原先输入的内容多了个(反斜杠)\,这时就需要用到stripslashes()了,它能把addslashes()处理时自动加上去的(反斜杠)\去掉。这跟区别于str_replace(“\”, “”,$str)不同。

当magic_quotes_gpc = Off时,使用addslashes()处理后的数据在数据库中将以’形式保存,不存在上面说的有\的问题,addslashes()起到插入数据不出错的作用,如果此时直接输出的话,数据正常。不需要再用stripslashes()。

addslashes()和stripslashes()正好是相反的,addslashes()加个(反斜杠)\,stripslashes()去个(反斜杠)\。
当magic_quotes_gpc = On时,系统会自动处理单引号等问题,用不用addslashes()和stripslashes()都没关系,但是如果添加数据时用了addslashes(),那么显示数据时必须要stripslashes()

当magic_quotes_gpc = Off时,系统不会处理单引号等问题,所以插入数据时必须要使用addslashes(),显示数据时则不需要使用stripslashes()。

既然有了分析,做程序时要怎么办呢?根据以上两种情况,可得:

不管magic_quotes_gpc是On还是Off,咱添加数据时都用addslashes(),当On时,必须使用stripslashes(),Off时则不能用stripslashes()。

如何判断On还是Off呢?用get_magic_quotes_gpc()。

【收录】PHP中stripslashes和addslashes的使用 有 42 条回应

  1. You should be a part of a contest for one of the most useful websites on the internet. I will highly recommend this site!

    回复
  2. If you are going for most excellent contents like me, just go to see this web site all
    the time as it presents feature contents, thanks

    回复
  3. I couldn’t resist commenting. Exceptionally well written!

    回复
  4. I am not sure where you’re getting your information, but good
    topic. I needs to spend some time learning more or understanding more.
    Thanks for wonderful information I was looking for this info for my mission.

    回复
  5. Hi! I could have sworn I’ve been to this website before but after browsing
    through some of the post I realized it’s new to me. Nonetheless, I’m definitely glad
    I found it and I’ll be bookmarking and checking back often!

    回复
  6. I am not sure where you’re getting your information, but good topic.
    I needs to spend some time learning more or understanding more.
    Thanks for excellent info I was looking for this info
    for my mission.

    回复
  7. of course like your web-site but you need to check the spelling
    on several of your posts. Several of them are rife with spelling issues
    and I to find it very troublesome to tell the truth nevertheless I will definitely come
    again again.

    回复
  8. If you desire to take much from this piece of writing then you
    have to apply these techniques to your won web site.

    回复
  9. Hi colleagues, its fantastic paragraph on the topic of educationand completely
    explained, keep it up all the time.

    回复
  10. I appreciate, result in I found exactly what I was taking a look
    for. You have ended my 4 day lengthy hunt! God Bless you man. Have a nice day.
    Bye

    回复
  11. Painter And Decorator Gloucester
    38 Bruton Way
    Gloucester GL1 1DA, United Kingdom
    01452 223268

    回复
  12. I simply couldn’t depart your site prior to suggesting that I actually enjoyed the standard information an individual supply on your guests? Is going to be again ceaselessly to inspect new posts.

    回复
  13. Aw, this was an exceptionally good post. Spending some time and actual effort to create a superb article… but what can I say… I procrastinate a whole lot and never seem to get anything done.|

    回复
  14. I quite like reading an article that can make men and women think.
    Also, thank you for allowing for me to comment!

    回复
  15. Every weekend i used to go to see this website, as i want enjoyment, for the reason that this this web page conations really nice funny
    data too.

    回复
  16. Everything is very open with a really clear description of the issues.
    It was truly informative. Your website is useful.

    Thank you for sharing!

    回复
  17. It’s very straightforward to find out any matter on net as compared to textbooks, as I found this post
    at this site.

    回复
  18. Thank you for the auspicious writeup. It in fact was a amusement account it.

    Look advanced to far added agreeable from you!
    However, how can we communicate?

    回复
  19. Hi there to every single one, it’s in fact a
    nice for me to pay a visit this web page, it includes important Information.

    回复
  20. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog?

    My blog site is in the very same niche as yours and my users
    would certainly benefit from some of the information you provide here.
    Please let me know if this alright with you. Thanks a lot!

    回复
  21. Howdy! 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 valuable information to work on. You have done
    a marvellous job!

    回复
  22. We’re a group of volunteers and starting a brand new scheme in our community.
    Your site offered us with useful information to work on. You have
    done a formidable job and our whole community can be thankful to
    you.

    回复
  23. Appreciate this post. Let me try it out.

    回复
  24. Hi there to all, for the reason that I am in fact keen of reading this webpage’s post
    to be updated on a regular basis. It includes nice stuff.

    回复
  25. I am sure this paragraph has touched all the internet visitors, its
    really really nice article on building up new weblog.

    回复
  26. What’s up everyone, it’s my first pay a quick visit at this
    web page, and paragraph is genuinely fruitful for me, keep up posting these articles.

    回复
  27. Hi, I do think this is an excellent website. I stumbledupon it ;
    ) I am going to revisit yet again since I book-marked it.
    Money and freedom is the greatest way to change, may
    you be rich and continue to guide other people.

    回复
  28. Keep this going please, great job!

    回复
  29. Does your site have a contact page? I’m having trouble locating it but, I’d like to send you an email.
    I’ve got some recommendations for your blog you might be interested in hearing.

    Either way, great site and I look forward to seeing
    it improve over time.

    回复
  30. Thanks for every other fantastic post. The place else may just anyone get
    that kind of information in such a perfect manner of writing?

    I’ve a presentation subsequent week, and I’m at the
    look for such info.

    回复
  31. Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn’t
    show up. Grrrr… well I’m not writing all that over
    again. Anyhow, just wanted to say fantastic blog!

    回复
  32. Hello to all, the contents existing at this site are really awesome
    for people experience, well, keep up the nice work fellows.

    回复
  33. Do you have a spam problem on this site; I also am a blogger, and I was
    wondering your situation; we have developed some nice methods and we are looking
    to swap solutions with others, please shoot me an email if interested.

    回复
  34. These are actually fantastic ideas in concerning blogging.

    You have touched some pleasant things here. Any
    way keep up wrinting.

    回复
  35. I all the time used to read piece of writing in news papers but now as I
    am a user of internet thus from now I am using net for articles, thanks to
    web.

    回复
  36. You have made some good points there. I
    looked on the net for additional information about the
    issue and found most people will go along with your
    views on this site.

    回复
  37. Fantastic site. A lot of helpful info here. I’m sending it
    to several buddies ans additionally sharing in delicious. And certainly, thanks to your sweat!

    回复
  38. Thanks for sharing your thoughts. I truly appreciate your
    efforts and I am waiting for your further post thank you
    once again.

    回复
  39. You can definitely see your enthusiasm within the article you write.
    The arena hopes for even more passionate writers such as you who aren’t afraid to say how they believe.
    All the time go after your heart.

    回复
  40. Definitely believe that which you said. Your favorite reason appeared
    to be on the net the simplest thing to be aware
    of. I say to you, I definitely get annoyed while people think about
    worries that they plainly don’t know about. You managed to hit the
    nail upon the top and defined out the whole thing without having side effect , people
    could take a signal. Will probably be back to get more. Thanks

    回复
  41. Do you have a spam issue on this website; I also am a
    blogger, and I was wondering your situation; we have created
    some nice methods and we are looking to exchange strategies with others, be sure
    to shoot me an e-mail if interested.

    回复
  42. Hello everybody, here every one is sharing these know-how,
    so it’s nice to read this weblog, and I used to pay a visit this website all the time.

    回复

发表评论

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