Understanding input escaping in PHP

前提是你 提交于 2019-12-05 16:04:40

Ah, the wonders of magic quotes. It is making those unnecessary escapes from your POST forms. You should disable (or neutralize) them, and many of your headaches go away.

Here's an exemplary article of the subject: http://www.sitepoint.com/blogs/2005/03/02/magic-quotes-headaches/

Recap: disable magic quotes, use real_escape_string().

Instead of relying on escaping I would use parametrized SQL queries and let the mysql driver do whatever escaping it needs.

It looks like your PHP server has the Magic Quotes feature enabled - that's where your first set of slashes comes from. In theory, it should then be unnecessary to call the escape functions - but when the app runs on a server with magic quotes disabled, you're suddenly wide open to SQL injection while thinking you aren't.

As chakrit wrote, escaping is not the best way to protect yourself - It's much safer to user parameterized queries.

What's going on is that you have Magic Quotes turned on in your PHP configuration.

It's highly recommended that youturn magic quotes off - in fact, they've been removed from PHP 6 completely.

Once you disable magic quotes, you'll see the POSTed text coming back exactly as you typed it in to the form: "Hello", said Jimmy O'Toole. It's now obvious that you need to use the mysql escaping functions or even better, prepared statements (with prepared statements you can't forget to escape a string as it's done for you).

Obvious is the keyword for a hacker.

I think escaping normally should be enough, but protecting against just the quotes might not be enough.

See this SQL Injection cheatsheet, it's a good list of test you can run and see if too many slahses is a good thing or not.

And don't forget to escape other kinds of values too, i.e. numeric fields and datetime fields can all be injected just as easily as strings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!