【问题标题】:Dissalow modification of hidden input when submit提交时不允许修改隐藏输入
【发布时间】:2015-09-10 19:36:36
【问题描述】:

我有一个问题,我现在不知道如何解释。所以我有一个输入类型 = hidden 和一个提交按钮的表单:

 <input type="hidden"
   name="{{ form_participate.total_amount.name }}"
   id="{{ form_participate.total_amount.name }}"
   value="{{ form_participate.total_amount.value }}"/>

问题是,当我使用 firebug 访问并删除 type = hidden 时,我输入了一个值,例如 1000,并且我确实提交了这个值被插入到数据库中。我可以禁止这个选项吗?

【问题讨论】:

  • 简短的回答是你不能。
  • 你能帮帮我吗?
  • 简答:你不能。
  • Cookies / Session - 简短回答 - onSubmit - 使用 from 发送它或在服务器端验证它。
  • 这简直是不可能的。

标签: javascript php jquery html css


【解决方案1】:

回答

您不能阻止用户临时修改客户端代码(HTML、JavaScript、CSS)。


替代方案

备选方案 #1 - 使用默认值进行清理

您可以清理和验证服务器端的值。

PHP

<?php
$value = $_REQUEST['myHiddenElement'];

// We make sure $value is a number and it its value is between 0 and 100
if(!is_numeric($value) || $value < 0 || $value > 100) {
    // If the value is invalid, we overwrite it with a default value.
    // This way, we're sure only valid values are sent to the server.
    $value = 0; 
}
?>

备选方案 #2 - 回馈用户

如果值无效,您还可以显示错误消息

PHP

<?php 
$value = $_REQUEST['myHiddenElement']

if(!is_numeric($value)) {
    if(!isset($_SESSION)) {
        session_start();
    }
    $_SESSION['error'] = 1;
    $_SESSION['error_message'] = "The value contained in [whatever input] is not valid.";
    header('Location: myForm.php'); // This goes back to the form.
}
?>

在 myForm.php 中:

<?php 
session_start();
// We show a custom error message only if the sesison variable "error" is set.
if(isset($_SESSION['error'])){
    echo '<div class="errorMessage">'.$_SESSION['error_message'].'</div>'; // Shows the message to the user.
    
    unset($_SESSION['error']); // unset() destroys the variables.
    unset($_SESSION['error_message']);
}
?>

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2013-10-18
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多