douben7493 2016-02-17 06:31
浏览 203
已采纳

你应该在isset()和empty()之前或之后使用htmlspecialchars()吗?

I was wondering what is the correct procedure when it comes to security and practicality. Should you use htmlspecialchars() on a variable before you check if it's set and empty?

Examples:

Use htmlspecialchars() on use or storage of post data:

$field = $_POST['field'];
if(isset($field)){
    //store or use: htmlspecialchars($field);
}

Or should you use htmlspecialchars() on retrieval of the post data:

$field = htmlspecialchars($_POST['field']);
if(isset($field)){
    //store or use: $field
}

This is probably a silly question, but I wanted to know which is correct.

  • 写回答

2条回答 默认 最新

  • duankuiyu4618 2016-02-17 06:35
    关注

    Well, think about it this way. Why do we use isset in the first place? The answer is to protect our code from trying to do things with something that doesn't exist.

    For example, using php -a in your console, you can see:

    php > $temp
    php > echo htmlspecialchars($temp);
    
    Parse error: parse error in php shell code on line 2
    php >
    

    Clearly you don't want your code throwing parse errors. So, you need to check that the value exists first (using isset), then do things with that value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测