douchuilai2355 2014-04-28 04:04
浏览 55
已采纳

如何检查下拉框是否与if语句匹配[PHP]

I have been coding a script, and I am stuck at one small part. I am checking to make sure that the form the user submits is secured. But I'm not sure how to approach the dropdown box, this is the code I have so far:

if ($_POST['type'] != '1' || $_POST['type'] != '2' || $_POST['type'] != '3') // Checks to see if the submitted type matches, prevents exploiting.
{
    $supportErrors[] = "Support type you've chosen was not found, possibly trying to exploit this script?";
}

I am not sure if this is the right way to check its value. This is my HTML side for this part of the script:

<select name="type" id="type" size="1" style="border: 1px solid #000000; background-color: #FFFFFF;">
      <option value="1" selected>Support</option>
      <option value="2">Question</option>
      <option value="3">Complaint</option>
</select>

Now, to my understanding, the value is the data being posted VIA 'type' - so, in my mind, shouldn't it post that value (1 or 2 or 3) and then in the PHP side, it checks if it isn't 1, 2 or 3?

  • 写回答

4条回答 默认 最新

  • douxiao0400 2014-04-28 04:08
    关注

    in_array() is a nice solution when checking if 1 thing matches one of multiple options

    if (!in_array($_POST['type'],array(1,2,3))){..}
    

    you should be developing with errors turned on:

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

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器