首页 > 解决方案 > 当我提交谷歌分析代码时,Codeigniter 验证失败

问题描述

当我从表单 .validation 提交分析代码失败时,此条件返回 false

if ($this->form_validation->run() == true) 

但是当我输入其他内容时,上述条件的分析代码将返回 true 并成功记录更新。即使将其留空,它也可以正常工作。我检查过可能是它的令牌问题。在配置文件中我设置为 false 它仍然不起作用,它提交数据,下面是我的 ajax 请求代码和控制器代码。

$config['csrf_protection'] = FALSE;

但是有线的东西相同的代码可以在其他服务器上运行。

 var csrfName="<?php echo $this->security->get_csrf_token_name();?>";

   var csrfHash="<?php echo $this->security->get_csrf_hash();?>";
$(document).ready(function(){
    CKEDITOR.replace('siteDescription',{allowedContent:true});
    $('#siteTags').tagsInput();
});
$(document).on('click','#updateBasic',function(e){  
$('#successSettings').addClass('hide').html('');
        $('#errorSettings').addClass('hide').html('');
        var description=CKEDITOR.instances.siteDescription.getData();
        var googleAnalyticsCode=$('#googleAnalyticsCode').val();
        var fbAppId=$('#fbAppId').val();
        var fbAppSecet=$('#fbAppSecet').val();
        var googleAppId=$('#googleAppId').val();
        var googleAppSecret=$('#googleAppSecret').val();
        var siteTags=$('#siteTags').val();
        var siteName=$('#siteName').val();
        var fb=$('#fb').val();
        var tw=$('#tw').val();
        var db=$('#db').val();
        var gp=$('#gp').val();
        var smtpUsername=$('#smtpUsername').val();
        var smtpPassword=$('#smtpPassword').val();
      var imgurClientId=$('#imgurClientId').val();

        var adminApproveQuestions=$('#adminApproveQuestions').is(":checked")?1:0;


        var fd = new FormData(); 
        fd.append('googleAnalyticsCode',googleAnalyticsCode);
        fd.append('fbAppId',fbAppId);
        fd.append('fbAppSecet',fbAppSecet);
        fd.append('googleAppId',googleAppId);
        fd.append('googleAppSecret',googleAppSecret);
        fd.append('description',description);
        fd.append('siteTags',siteTags);
        fd.append('siteName',siteName);
        fd.append('smtpUsername',smtpUsername);
        fd.append('smtpPassword',smtpPassword);
        fd.append('imgurClientId',imgurClientId);
        fd.append('adminApproveQuestions',adminApproveQuestions);  
        //fd.append('adminApproveAnswers',adminApproveAnswers);  
        fd.append('fb',fb);
        fd.append('tw',tw);
        fd.append('db',db);
        fd.append('gp',gp);
        fd.append(csrfName,csrfHash);
        $.ajax({
            type:'POST',
            url:'<?php echo base_url()."post-update-settings"?>',
            type: "POST",
            data : fd,
            processData: false,
            contentType: false,
            success: function(response) 
            {
                var response=$.parseJSON(response);
                if(response['type']==1)
                {
                    $('#successSettings').html(response['html']);
                    $('#successSettings').removeClass('hide');
                }
                else
                {
                    $('#errorSettings').html(response['html']);
                    $('#errorSettings').removeClass('hide');
                }
            },
            error: function (xhr, data, error) {
                if (window.confirm("This page is expired , Please click Yes to reload the page"))
                {
                    window.location.reload(true);
                }
            }
        });
    });

控制器代码

public function postUpdateSiteSettings()
    {
        if (!checksessionAdmin()) {
            responseGenerate(2,"Please login to continue");
        }
        $fb=secureInput($this->input->post('fb'));
        $gp=secureInput($this->input->post('gp'));
        $db=secureInput($this->input->post('db'));
        $tw=secureInput($this->input->post('tw'));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('siteName', 'Site Name', 'required|max_length[100]');
        $this->form_validation->set_rules('siteTags', 'Site Tags', 'max_length[100]');

        if (strlen($fb)>0)
        $this->form_validation->set_rules('fb', 'Facebook social link', 'max_length[200]|callback_valid_url');
        if (strlen($gp)>0)
        $this->form_validation->set_rules('gp', 'Google plus social link', 'max_length[200]|callback_valid_url');
        if (strlen($db)>0)
        $this->form_validation->set_rules('db', 'Dribble social link', 'max_length[200]|callback_valid_url');
        if (strlen($tw)>0)
        $this->form_validation->set_rules('tw', 'Twitter social link', 'max_length[200]|callback_valid_url');

        $this->form_validation->set_rules('smtpUsername', 'SMTP Username', 'max_length[200]');
        $this->form_validation->set_rules('smtpPassword', 'SMTP Password', 'max_length[200]');

        if ($this->form_validation->run() == true) {
            $siteName=secureInput($this->input->post('siteName'));
            $siteTags=secureInput($this->input->post('siteTags'));
            $adminApproveQuestions=secureInput($this->input->post('adminApproveQuestions'));



            $googleAnalyticsCode=(trim($this->input->post('googleAnalyticsCode')));
            $fbAppId=secureInput($this->input->post('fbAppId'));
            $fbAppSecet=secureInput($this->input->post('fbAppSecet'));
            $googleAppId=secureInput($this->input->post('googleAppId'));
            $googleAppSecret=secureInput($this->input->post('googleAppSecret'));
            $description=encodeContent(trim($this->input->post('description')));
            $smtpUsername=secureInput($this->input->post('smtpUsername'));
            $smtpPassword=secureInput($this->input->post('smtpPassword'));
            $imgurClientId=secureInput($this->input->post('imgurClientId'));
            $this->App_model->updateData('siteSettings',['smtpUsername'=>$smtpUsername,'smtpPassword'=>$smtpPassword,'imgurClientId'=>$imgurClientId,'adminApproveQuestions'=>$adminApproveQuestions, 'googleAnalyticsCode'=>$googleAnalyticsCode,'fbAppId'=>$fbAppId,'fbAppSecet'=>$fbAppSecet,'googleAppId'=>$googleAppId,'googleAppSecret'=>$googleAppSecret,'facebookLink'=>$fb,'googleLink'=>$gp,'twitterLink'=>$db,'dribbleLink'=>$tw,'siteName'=>$siteName,'tags'=>$siteTags,'description'=>$description]);
            responseGenerate(1,"Site settings was successfully updated");
        } else {
            responseGenerate(0,validation_errors('<p>','</p>'));
        }
    }

标签: jqueryajaxcodeigniter

解决方案


推荐阅读