用于数据库和网络安全的 preg_replace 函数
出于安全目的,我在面向对象的程序中使用 preg_replace 函数来删除字符。但是,该字段需要允许某些字符,例如 @ 或数字等。我可以在 preg_replace 函数中放置什么来保护我的数据库,但允许用户输入适当的数据。这些数据如电话号码、姓名、用户名等。密码使用 MD5 功能加密。
有人告诉我使用 md5 和 preg_replace 是个坏主意。如果可以,我可以使用哪些功能?
I was using a preg_replace function in a object oriented program to remove characters for security purposes. However the field will need to allow certain characters like @ or numbers etc. What can I place within a preg_replace function to secure my database but allow users to enter appropriate data. This data such as phone number, name, username, etc. The password is encrypted using MD5 Function.
I was told that using md5 and preg_replace were bad ideas. If so, what functions are available for me to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了保护 batabase,请使用
mysql_real_escape_string
、mysqli_real_escape_string
、PDO 参数绑定,具体取决于您使用的内容。在放入数据库之前无需用preg_replace
替换任何内容。To secure batabase use
mysql_real_escape_string
,mysqli_real_escape_string
, PDO parameters binding depending on what you use. No need to replace anything withpreg_replace
before placing in database.仅仅依靠 preg_replace 来保证安全并不是一个好主意。不过,它对于强制执行/标准化电话号码或日期格式等内容很有用。
MD5 和 SHA1 等算法用于散列。您可以在任何数据上使用它,甚至是危险的不受信任的输入,并且您总是会得到一个十六进制字符串。但是,由于哈希是单向的,因此您无法将它们“解密”回原始输入。
@dfsq 是对的 - 使用这些方法将潜在危险的输入安全地保存到数据库中。您想要使用哈希的唯一情况是密码之类的东西,没有人应该能够解密它们。 (如果您对密码进行哈希处理,请确保也对它们加盐!)
Relying solely on preg_replace for security isn't a good idea. It can be useful for enforcing/standardizing things like phone number or date formats though.
Algorithms like MD5 and SHA1 are for hashing. You can use it on any data, even dangerous untrusted input, and you'll always get a hexadecimal string. However, because hashes are one-way, you cannot "decrypt" them back to the original input.
@dfsq is right - use those methods to securely save potentially dangerous input to your database. The only case where you'd want to use a hash is for something like passwords, where nobody should ever be able to decrypt them. (And if you do hash your passwords, make sure you salt them as well!)