douzhongqiu5032 2017-06-15 03:38
浏览 169
已采纳

我是否需要在html标记属性中使用htmlentities()来防止XSS攻击

So suppose I have this piece of code:

<div class="name" id="<?php echo $userId; ?>">      
    <p>User Name: <?php echo $username; ?></p>
    <p>Last Activity: <?php echo getTime($datetime); ?></p>
    <p>Date: <?php echo $date; ?></p>
</div>

In this, do I have to write htmlentities inside the div id attribute too?

For example, like this:

<div class="name" id="<?php echo htmlentities($userId); ?>" >...</div>

Also, do I need to do this:

<p>Last Activity: <?php echo htmlentities(getTime($datetime)); ?></p>

Thanks for the help!

P.S.:datetime is a function that I have created

  • 写回答

1条回答 默认 最新

  • dongyukang7006 2017-06-15 03:52
    关注

    It depends on if the user can manipulate those variables or not. If they can prior to them being echoed out, then you should sanitize those variables (using htmlentities() and others as well). If those variables are hard coded in so users cannot manipulate them, then you are fine.

    Basically, can the user alter the variable $userID in any way prior to echo $userID being called? Then, yes, you should include htmlentites and probably some other things as well for security purposes.

    Here is a link that may help. Be sure to read the comments for the answers as well.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料