【问题标题】:Passing escaped query string with html special chars between two pages在两个页面之间传递带有 html 特殊字符的转义查询字符串
【发布时间】:2014-04-12 14:03:51
【问题描述】:

我正在尝试传递一个包含特殊 html 字符的查询字符串(例如 <)。我必须这样做

window.location.href = ".."

在另一页上,我必须使用 PHP 检索此查询字符串。但是当我使用 isset() 检查它时,它返回 false!

例如,当我需要像这样使用 JS 转义 <p> 时:

function HtmlEncode(s)
      {
          var el = document.createElement("div");
          el.innerText = el.textContent = s;
          s = el.innerHTML;
          return s;
      }

window.location.href = "http://localhost/test.php?t="+HTMLEncode("<p>");

现在网址是:http://localhost/test.php?t=&amp;lt;p&amp;gt;

当我执行echo isset($_GET["t"]); 时,我得到false 结果。

或者即使我尝试this is a &lt;p&gt; tag,我得到$_GET["t"] 等于this is a

谁能告诉我发生了什么?

【问题讨论】:

    标签: javascript php escaping query-string


    【解决方案1】:

    【讨论】:

    • encodeURIComponent() 不会转义特殊的 html 字符。
    • @RafaelAdel jsfiddle.net/BsK8r 。您的 URL 中不能包含 &amp;lt;&amp;gt;,因为 &amp; 是 URL 中的特殊字符,因此您必须对其进行 URI 编码。
    • 嗯,但是为什么我的网址保持不变,我使用encodeURIComponent(),例如&amp;t=&lt;p&gt; ?
    • @RafaelAdel 哪个浏览器?
    • 我正在 Chrome 和 FF 上进行测试。
    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 2012-03-23
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2020-03-18
    • 2014-02-16
    相关资源
    最近更新 更多