doujiao3998 2013-09-23 20:13
浏览 30
已采纳

PHP`rawurlencode`和JS`codingURIComponent`之间的不同行为是否重要?

According to https://stackoverflow.com/a/1734255/1529630, encodeURIComponent is the same as rawurlencode, but !*'() aren't escaped, e.g.,

function encodeURIComponent($str) {
    $revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
    return strtr(rawurlencode($str), $revert);
}

But then, does it matter that difference?

Normally, I use something like

  • In JS
    wrapper.innerHTML = '<a href="foo.php?bar=' + encodeURIComponent(myVar) + '">Link</a>';
  • In PHP
    echo '<a href="foo.php?bar=' . rawurlencode(myVar) . '">Link</a>';

If then, in foo.php, I use $_GET['bar'], is it possible to get different results, due to the difference between encodeURIComponent and rawurlencode?

  • 写回答

1条回答 默认 最新

  • dongzong1866 2013-09-23 21:34
    关注

    You only need to escape characters that can have special uses within the code.

    For example the following can be used to ask the code to do a mathematical comparison or calcuation - < , > , + , - , / , =

    then there's reserved characters specific to URL creation such as - ? , @ , %, #

    The characters !*'() have no special meaning and so won't be misinterpreted so don't need escaping. You can however escape characters unnecessarily so it might look like a different result, but it would mean/do the same thing.

    This has a more thorough breakdown - http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址