duandgfh0506 2014-02-27 09:06
浏览 37
已采纳

PHP表单提交和表单数据验证

In PHP is this enough to guarantee a form has been submitted by clicking the form submit button and to verify the content posted is not empty?

if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['field_data']))
{
echo "ok";
}
  • 写回答

3条回答 默认 最新

  • dongshou1991 2014-02-27 09:20
    关注

    I think, there may be a way to be sure the form was submitted using your form.
    If I would like to do it, I think I will make something like this :

    $secure = $_SESSION['form']['submit'] = MD5(time());
    
    <form>
    <input type='hidden' name='secure_form' value='<?php echo $secure ?>' />
    </form>
    

    And else when submitted check the value :

    if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['secure_form']) && $_SESSION['form']['submit'] ==  $_POST['secure_form']) {
      //do stuff
    }
    

    Of course, you have to add session_start() at the top of the page!

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

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退