Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

php - Is this a safe/strong input sanitization function?

This is the sanitization function used in a book I recently learned from - Sams Teach Yourself Ajax, JavaScript, and PHP All in One.

I've been using it on my own PHP site. Is it safe for real-world usage?

function sanitizestring($var)
{
  $var = strip_tags($var);
  $var = htmlentities($var);
  $var = stripslashes($var);
  return mysql_real_escape_string($var);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would say that is too general. It may be safe for a lot of uses, but it would often give unwanted side affects to strings. Not every string should be escaped like that.

  • mysql_real_escape_string() should be used within SQL queries only. Better still, bind params with PDO.
  • Why would you want to blanket strip tags and encode entities before inserting into a database? Maybe do it on the way out.
  • For XSS prevention, htmlspecialchars() is more of your friend. Give it the character set as an argument.

So I would use mysql_real_escape_string() for queries, and htmlspecialchars() for echoing user submitted strings. There is also a lot more to know. Do some further reading.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...