Jquery 带有ajax的codeigniter csrf在IE中不起作用
我使用codeigniter启用了csrf,它在FireFox和Google Chrome中正常工作。但是,在IE中,它在web developer工具网络面板中显示此错误: 详细地说: 我的Jquery 带有ajax的codeigniter csrf在IE中不起作用,jquery,ajax,codeigniter,internet-explorer,csrf,Jquery,Ajax,Codeigniter,Internet Explorer,Csrf,我使用codeigniter启用了csrf,它在FireFox和Google Chrome中正常工作。但是,在IE中,它在web developer工具网络面板中显示此错误: 详细地说: 我的$。post呼叫是: var ctn = $.cookie('csrf_cookie'); $.post('some_path', { 'my_token': ctn }, function (data) { if (data.res == 'something') {
$。post
呼叫是:
var ctn = $.cookie('csrf_cookie');
$.post('some_path', {
'my_token': ctn
}, function (data) {
if (data.res == 'something') {
//process here
}, 'json');
当我执行console.log('ctn:'+ctn)
时,会正确显示ctn
的值,该值是保存CSRF令牌值的cookie:
ctn: 78346b5d0ec105efcce796f93ecc3cbb
任何帮助或建议调试更多将不胜感激
附言:我有一个vhost
,我真的不知道它是否与IE有区别
更新:
我在IE中读到一些建议使用P3P标题的CSRF问题,因此我将此标题添加到索引页:
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT');
但还是有同样的问题
还有其他建议吗?如我在中所建议的。最好使用net.tutsplus中标题为:。这是一篇老文章,解决方案是针对Codeignter 1.7.x的。然而,这是迄今为止我能找到的唯一适用于Codeigniter 2的解决方案,以便在Codeigniter中实现CSRF保护。我们这里的主要问题是Codeigniter使用COOKIE。然而,这篇文章使用了Codeigniter会话,它使用起来更安全,并且可以在所有浏览器上正常工作,没有任何问题。来自net.tutsplus的文章将适用于AJAX和非AJAX请求
因此,我的建议是,如果你没有找到解决问题的方法,你可以尝试执行这篇文章:感谢@John建议我自己执行CSRF,我通过了第一期。然而,事实证明IE根本没有提交post数据(在调试之后),因此,有一个关于标题stackoverflow的问题 为了解决这个问题,你必须添加这个
meta
标签,告诉IE在IE9兼容模式下使用javascript
<meta http-equiv="x-ua-compatible" content="IE=9" >
您是否尝试过为请求设置contentType:
“application/json”
?旁注:我遇到了许多问题,这些问题都来自IE缓存ajax请求……我已经尝试过了。不工作顺便说一句,IE正在捕获请求,但由于csrf未被验证,它返回了500。我在csrf中遇到了相同的问题,因此我直接在php文件中使用js,并在ajax中传递数据,如数据:{'':'',''开始]:转换(开始),'结束]:转换(完),“全天”:全天,“标题”:“标题”},对于meso来说,它的工作很好,你用杂货店积垢实现了哪种方式。因为GC是在IE中使用csrf实现的。+1,john-我尊重你。因此,我尊重你的意见。在IE9+IE10中,它工作得很好,这就是为什么我问你,所以我可能有能力在我的代码中应用相同的方式。无论如何,谢谢你的回答,我会保留我的问题tion现在开放,看看是否有其他人想添加它。嗨,我确实实现了它,并且我发布了一个解释确切问题的答案。谢谢你的帮助。:DHello@mamdouhalramadan,我真的很高兴我能帮助你完成这一任务。我也更高兴的是,在这个如此常见的问题上终于有了一个正确的答案“codeigniter csrf with ajax在IE中不起作用?”。我认为你的回答也会帮助其他人。是的,当然,所有关于杂货店积垢的社交媒体都在:;-)的页脚右侧有fb、g+、twitter和github:)
<?php
/**
* Description of csrf_protection
* @author Ian Murray
*/
class Csrf_Protection {
private $CI;
private static $token_name = 'somename';
private static $token;
public function __construct() {
$this->CI = &get_instance();
}
/**
* Generates a CSRF token and stores it on session. Only one token per session is generated.
* This must be tied to a post-controller hook, and before the hook
* that calls the inject_tokens method().
*
* @return void
* @author Ian Murray
*/
public function generate_token()
{
// Load session library if not loaded
$this->CI->load->library('session');
if ($this->CI->session->userdata(self::$token_name) === FALSE)
{
// Generate a token and store it on session, since old one appears to have expired.
self::$token = md5(uniqid() . microtime() . rand());
$this->CI->session->set_userdata(self::$token_name, self::$token);
}
else
{
// Set it to local variable for easy access
self::$token = $this->CI->session->userdata(self::$token_name);
}
}
/**
* Validates a submitted token when POST request is made.
*
* @return void
* @author Ian Murray
*/
public function validate_tokens()
{
// Is this a post request?
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// Is the token field set and valid?
$posted_token = $this->CI->input->post(self::$token_name);
if($posted_token === FALSE){
$posted_token = $this->_get_token_in_post_array($this->CI->input->post());
$this->_check_all_post_array($posted_token);
}
}
}
/**
*takes the posted token and check it after multidimesional-array search
*@params $posted_token
*@author Mamdouh Alramadan
*/
private function _check_all_post_array($posted_token)
{
if ($posted_token === 'error' || $posted_token != $this->CI->session->userdata(self::$token_name))
{
// Invalid request, send error 400.
show_error('Request was invalid. Tokens did not match.', 400);
}
}
/**
* This injects hidden tags on all POST forms with the csrf token.
* Also injects meta headers in <head> of output (if exists) for easy access
* from JS frameworks.
*
* @return void
* @author Ian Murray
*/
public function inject_tokens()
{
$output = $this->CI->output->get_output();
// Inject into form
$output = preg_replace('/(<(form|FORM)[^>]*(method|METHOD)="(post|POST)"[^>]*>)/',
'$0<input type="hidden" name="' . self::$token_name . '" value="' . self::$token . '">',
$output);
// Inject into <head>
$output = preg_replace('/(<\/head>)/',
'<meta name="cname" content="' . self::$token_name . '">' . "\n" . '<meta name="cval" content="' . self::$token . '">' . "\n" . '$0',
$output);
$this->CI->output->_display($output);
}
/**
* takes the posted array and check for the token inside it
* @params $arr array
* @author Mamdouh Alramadan
*/
private function _get_token_in_post_array($arr)
{//this function is customized to my case but it's easy to adapt
if(is_array($arr)){
$key = $this->_recursive_post_array_search(self::$token_name, $arr);//this will return data if token found
if($key === 'data'){//I'm expecting the token inside data array
$key = $this->_recursive_post_array_search(self::$token_name, $arr['data']);
return isset($arr['data'][$key]['value'])?$arr['data'][$key]['value']:FALSE;
}
}
return 'error';
}
//some custom function to do multi-dimensional array search, can be replaced with any other searching function.
private function _recursive_post_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && $this->_recursive_post_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
}
?>