douxing7101 2015-08-06 15:18
浏览 252

Csrf令牌仅在第一次使用ajax提交时有效

i want to submit my form using jquery ajax, it is ok but just for first time i click submit, here is my code.

    <!-- input code from the form -->
    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>" />

    <!-- js code -->
    $.ajax({
        url: ajaxurls.ask,
        type: 'POST',
        data: formData,
        contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false, 
        success: function (html) {
            var data = jQuery.parseJSON(html);
            if(data.status == 'ok') {
                $('.row_question_form').after(data.response.html);
                $('.question_' + data.response.question[0].question_id).hide().fadeIn();
                $('#question_form').val('');
                $('#thumbnails').empty();
                $('#ask_question_messages').empty();
                $('#ask_question_messages').html(data.message);
            }else if(data.status == 'error'){
                $('#ask_question_messages').empty();
                $('#ask_question_messages').html(data.message);
            }
        }
  });

You would probably say to set $config['csrf_regenerate'] = FALSE; but in this case someone could create hundred of records in my database with a application like this:

http://i.imgur.com/qA4pqyr.png

  • 写回答

1条回答 默认 最新

  • doudang4857 2015-08-07 02:22
    关注

    It is correct that you can only submit one POST per CSRF Token, as they are for one-time use.

    If your intention is to alter serverside data, POST is the correct method.

    But if you are trying to just read data from server, you should use GET method.

    This is nicely explained here.


    If you want to disable CSRF renewal because it would not be necessary to generate a new token per request you can do it in CI v3.0 by setting the configuration:

    $config['csrf_regenerate'] = FALSE;
    

    If you, on the other hand, prefer to get a new token and refresh your form for a new submission, read this howto.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀