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

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

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

报告相同问题?

悬赏问题

  • ¥20 有没有会写mysql的,有偿做个问题
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?