【问题标题】:how to protect hidden field in laravel when posting the form in laravel 5.2在 laravel 5.2 中发布表单时如何保护 laravel 中的隐藏字段
【发布时间】:2016-05-18 06:18:37
【问题描述】:

我正在使用 laravel 5.2 开发小型网站(现在添加照片),我想更新 现有的(通过弹出引导模式并为当前照片选择新的,我在处理ajax请求时像这样)

   $('select#choose_update_photo').on('change',function() {

                  $category_id =  $(this).val(); // category_id for the photo

                    $.ajax({

                        type : 'post' ,

                        url: '{{ url('/ChoosePhotos') }}',
                            data : {id : $category_id ,
                            "_token": token },


                        success: function(data) { ... });

到给定的路线:

// the route for making post request for getting photos

    Route::post('ChoosePhotos','PhotosController@choosePhoto');

这是回应:

public  function  choosePhoto () {


        if(request()->ajax())
        {

            $result = request()->input('id');


             $all_photos = $this->photo->RetreiveAllPhotos();
            return response()->json($all_photos) ;
        }

    }

当我点击任何检索照片时,我关闭模式 并存储(检索到的 (image_id) 在我的表单中的隐藏输入中)

像这样:

但是我发现如果我像这样手动更改隐藏(id):

<input type="hidden" name="pop_photo_hidden" value="20" id="hide_photo_id">

它会发布 到我的控制器来保存它,,,这会毁了我所有的工作 如何处理,还有另一种发送数据的方法(在我的情况下是 photo_id),而不让用户触摸它们或伪造我就是它)

【问题讨论】:

    标签: php ajax laravel hidden


    【解决方案1】:

    在您的模型中将 photo_id 定义为像这样受到保护。

    protected $guarded=['photo_id'];
    

    【讨论】:

      猜你喜欢
      • 2015-06-13
      • 1970-01-01
      • 2016-12-03
      • 2016-12-05
      • 2012-03-04
      • 2019-02-04
      • 2018-05-27
      • 2015-09-21
      • 2016-12-29
      相关资源
      最近更新 更多