CodeIgniter CSRF 403错误
我试图解决这个问题很长一段时间,并没有丝毫的线索。 我想在CodeIgniter框架内发送和捕获AJAX请求。 与页面tamplate PHP文件:CodeIgniter CSRF 403错误
<table id="jivoClients" class="display nowrap" cellspacing="0" width="100%">
<tbody>
<?php foreach($clients as $client): ?>
<td class="details-control" id="<?php echo $client["chat_id"]. ","
.$this->security->get_csrf_token_name(). ","
.$this->security->get_csrf_hash(); ?>"></td>
<th class="clientData"><?php echo str_replace(",", "<br>" , $client["visitor_info"]); ?></th>
<th class="clientData"><?php echo ($client['session_geoip_country']); ?></th>
<th class="clientData"><?php echo ($client['session_geoip_city']); ?></th>
<th class="clientData"><?php echo ($client['visitor_chats_count']); ?></th>
<th class="clientData"><?php echo ($client['agents_names']); ?></th>
<th class="clientData"><?php if(isset($client['messages']['0']['timestamp'])): ?>
<?php echo date('m/d/Y', $client['messages']['0']['timestamp']); ?>
<?php endif;?></th>
<th class="clientData"><?php if(isset($client['messages']['0']['timestamp'])): ?>
<?php echo date('H:i:s', $client['messages']['0']['timestamp']); ?>
<?php endif;?></th>
<th class="clientData"><?php if(isset($client['messages']['0']['Message'])): ?>
<?php echo ($client['messages']['0']['Message']); ?>
<?php endif;?></th>
<th class="clientData"><?php echo ($client['Manager_note']); ?></th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
在我的js文件的代码如下所示:
var id = $(this).attr('id');
var messageData = id.split(",");
var token = "" + messageData[1];
var post_data = {
'id' : messageData[0],
'csrf_crm' : messageData[2]
}
$.ajax({
type:'POST',
data: {
func : 'getNewLocations',
'id' : messageData[0],
'csrf_crm' : messageData[2]
},
url:'application/controllers/AjaxController.php',
success: function(result, statut) {
if (result == 'add') {
//do something
}
else if (result == 'remove') {
//do something
}
}
});
而且我不断收到403错误。正如我已经在论坛上阅读的那样,这意味着我的CSRF令牌不正确。但似乎我通常得到它(在调试器中检查)。 但是这并不能解决问题。 在此先感谢。
最后我解决了这个问题。 它显示,我在js文件中使用不正确的路线。 的动作的正确序列看起来像这样: 1)在文件routes.php文件添加新行:
$route['jivosite/user'] = 'jivosite/user';
2)在js文件添加代码: post_data = JSON.stringify(post_data);
post_data = JSON.stringify(post_data);
var url = baseurl + "jivosite/user"
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.addEventListener("readystatechange", function() {
if (xhr.readyState == 4 && xhr.status === 200) {
console.log(xhr.response);
}
});
xhr.addEventListener("error", function (err) {
console.log(err);
});
xhr.send(post_data ? JSON.stringify(post_data) : null);
3)检查config.php文件。 $config['cookie_secure'] = FALSE;
应该是这样的 和$config['csrf_exclude_uris'] = array('jivosite/user');
应包括我的路线
4)控制器文件夹中创建文件Jivosite.php与内容:
class Jivosite extends CI_Controller
{
public function user()
{
$id=$this->input->post('id');
echo $id;
}
}
而且它为我工作。 感谢您的所有答案。
您需要做echo
来指定php变量的值。此外包裹令牌''
var token = '<?php echo $this->security->get_csrf_hash() ?>'; //<----- do echo here
var id = $(this).attr('id');
$.ajax({
type:'POST',
data: {
func : 'getNewLocations',
'id' : id,
'csrf_crm' : token
},
url:'application/controllers/AjaxController.php',
success: function(result, statut) {
if (result == 'add') {
//do something
}
else if (result == 'remove') {
//do something
}
}
});
感谢您的纠正,但它并没有解决主要问题 - toen不被CodeIgniter接受 –
将'csrf_crm'改为' security-> get_csrf_token_name(),?>'然后尝试。我也怀疑'func:' –
不能将php代码插入到js文件中。所以我从配置文件复制了名字并将其插入到数据数组中。至于功能,现在不要太在意它,因为它不能正常工作。 –
获取CSRF令牌值是这样的:
var token = $('[name="csrf_token"]').val();
谢谢。 Interresting获取令牌名称的方式。但在我看来,这不是问题,因为令牌发送不正确。 –
还有两件事你可以申请并交叉检查1.网址(也可以尝试附加baseurl),2。将'csrf_crm'更改为'csrf_token' –
1)将'csrf_crm'更改为'csrf_token' - 无结果 –
在配置文件集$ config ['csrf_regenerate'] = TRUE;到$ config ['csrf_regenerate'] = FALSE; –
确保您的配置文件中的csrf令牌重新生成为false,否则它将在每个请求中重新生成一个新令牌。 $ config ['csrf_regenerate'] = FALSE; –
做到了。没有结果 –