安全地编码字符串并将其从 html 链接传递到 PHP 程序
需要采取哪些系列步骤才能安全编码和 使用 javascript 从 html href 传递一个字符串来构建指向 php 程序的链接。
在 javascript 中设置
// encodes a URI component.
path = "mypgm.php?from=" + encodeURIComponent(myvar) ;
php 中的 URL:
// get passed variables
$myvar = isset($_GET['myvar']) ? ($_GET['myvar']) : '';
// decode - (make the string readable)
$myvar = (rawurldecode($myvar));
// converts characters to HTML entities (reduce risk of attack)
$myvar = htmlentities($myvar);
// maybe custom sanitize program as well?
// see [http://stackoverflow.com/questions/2668854/php-sanitizing-strings-to-make-them-url-and-filename-safe][1]
$myvar = sanitize($myvar);
What series of steps would be reqired to safely encode and
pass a string from a html href using javascript to construct the link to a php program.
in javascript set up URL
// encodes a URI component.
path = "mypgm.php?from=" + encodeURIComponent(myvar) ;
in php:
// get passed variables
$myvar = isset($_GET['myvar']) ? ($_GET['myvar']) : '';
// decode - (make the string readable)
$myvar = (rawurldecode($myvar));
// converts characters to HTML entities (reduce risk of attack)
$myvar = htmlentities($myvar);
// maybe custom sanitize program as well?
// see [http://stackoverflow.com/questions/2668854/php-sanitizing-strings-to-make-them-url-and-filename-safe][1]
$myvar = sanitize($myvar);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为前两行应该没问题。如果必须将其输出为文本,则可以使用 htmlentities。
I think the first two lines should be fine. You would use htmlentities if and when you have to output it as text.
查看您的代码,您真正需要的是:
除此之外,PHP 还会自动进行 URL 解码。我个人更喜欢在输出数据时执行
htmlentities()
或htmlspecialchars()
,即:您特别需要转义或清理数据的唯一其他时间是如果您正在构建一个 SQL 查询:
这将防止 SQL 注入。除非您正在格式化用户输入或执行验证,否则绝对没有必要进行任何其他类型的清理。
希望这有帮助!
Looking at your code, all you really need is this:
Beyond that, PHP automatically URL decodes. I personally prefer to do my
htmlentities()
orhtmlspecialchars()
when I go to output data, i.e.:The only other time you specifically need to escape or sanitize data is if you're building a SQL query:
That will prevent SQL injection. Unless you're formatting user input or performing validation, it's not absolutely necessary to do any other kind of sanitization.
Hope this helps!