dongshui2254 2012-04-11 22:15
浏览 24
已采纳

在用户输入后输出SESSION - 防止xss

I have this code to echo out a username of a user that has just logged in:

echo $_SESSION['user']['username']."

Would I be right in thinking that if I change my code to:

echo htmlspecialchars $_SESSION['user']['username']."

i am protecting myself from low level XSS atleast?

  • 写回答

2条回答 默认 最新

  • douduan5753 2012-04-12 08:31
    关注

    The function htmlspecialchars does not encode single quotation(') by default, if your user name echo in an html attribute or inside javascript, there would be XSS!

    For example:

    <script>
    name='$YOUR_NAME_HERE$';
    </script>
    

    We can set the user name to ';alert('xss');// So the browser will generate the html content like this

    <script>
    name='';alert('xss');//';
    </script>
    

    And my advice to you is:

    echo htmlspecialchars($_SESSION['user']['username'], ENT_QUOTES);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分