转义数据的最佳方式 JS->PHP->MySQL 反之亦然
为了以下目的,我必须使用哪些函数来编码/解码/转义/stripslash 数据?
- 当从 JS 调用 PHP 脚本时,例如:
page.php?data=dont_use_#_and_%_in_URL_params
- 当 PHP 脚本从 JS 接收参数时,例如:
don%27t_use_%23_and_%25_in_URL_params
- 接收的数据来防止 MySQL 注入(假设我需要在数据库中插入以下字符序列:
"``'
) - 当从 PHP 运行 MySQL 查询时,使用之前从JS "``' 序列的字段值与表达式进行比较
- 当我需要从 MySQL 表中检索字段值并且该字段包含
"``' 时,需要在 MySQL 语句中将包含
我想在 PHP eval() 宏替换中使用它
- ,当我必须在 AJAX 响应中将数据从 PHP 发送到 JS 并且它包含
"``'
字符时 - ,最后我有eval() 之前在 JS 中的响应
类似于下图:
JS(encode) --> (解码)PHP(编码) --> (解码?)MySQL(编码?) --> (解码)MySQL(编码) --> (解码)JS
如果有人有时间和乐意回答,或者如果我在这里犯了任何错误,请纠正我,提前致谢
what functions i have to use to encode/decode/escape/stripslash data for following purposes?
- when calling a PHP script from JS like:
page.php?data=don't_use_#_and_%_in_URL_params
- when a PHP script receive a parameter from JS like:
don%27t_use_%23_and_%25_in_URL_params
- when running a MySQL query from PHP with data previously received from JS to prevent MySQL injections (lets say i need to insert in database the following sequence of characters:
"``'
) - when i need to compare in a MySQL statement a field value which contains
"``'
sequence with an expression - when i need to retrieve a field value from a MySQL table and the field contains
"``'
and i want to use it in a PHP eval() macrosubstitution - when i have to send data from PHP to JS in an AJAX response and it contains
"``'
characters - and finally i have to eval() previous respond in JS
something like this diagram:
JS(encode) --> (decode)PHP(encode) --> (decode?)MySQL(encode?) --> (decode)MySQL(encode) --> (decode)JS
if anyone have the time and pleasure to answer, or to correct me if i made any mistakes here, thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$_GET
eval
的味道很糟糕。eval()
是 JS 的时候是当您在没有本机版本的浏览器中实现对json
的支持时(并且您可以使用 Crockford 的 json2.js为此)。所以不要。$_GET
eval
smells bad though.eval()
is JS is when you are implementing a support forjson
in browsers without a native version (and you can use Crockford's json2.js for that). So don't.3-4.数据源在这里并不重要。构建查询有一些通用规则,我相信您已经知道了。如果没有 - 请参阅 完整的解释。
3-4. Data source doesn't matter here. there are common rules for building the query, I am sure you know it all already. If not - refer to this complete explanation.