由于这个问题,我停止了我的codeigneiter应用程序。我正在使用ajax(jquery)作为用户注册脚本,但是当连接到控制器时我收到错误500内部服务器错误,我读到问题与CSRF有关,但即使我关闭了CSRF,问题仍然存在配置文件。
我的ajax代码是:
$(document).ready(function(){
var controller = 'Auth';
var base_url = '<?php echo site_url(); ?>';
// User Registration
$('#subm').click(function()
{
var username = $("#username").val();
var email = $("#email").val();
var password = $("#password").val();
var dataString = $('#registerForm').serialize();
if(username.length < 6 || username.length > 20)
{
$('#loader').hide();
$('#errormsg').html("username length must be between 6 and 20 characters .") ;
$('.error-register').show();
}
// If everything is OK
else
{
$.ajax({
type: "POST",
url: base_url + controller + "/register",
data: dataString,
cache: false,
success: function(message)
{
$('#loader').hide();
if(message == true)
{
$("#successModal").modal();
$('.notify').html("You have successfully Registered , Please Confirm Your Email !");
$("#username,#email,#password").val('');
}
else
{
$('#errormsg').html(message) ;
$('.error-register').show();
}
}
});
}
return false;
//End click
});
//End ready
});
Auth控制器代码为:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends CI_Controller
{
public function register()
{
$email = $this->input->post('email');
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->form_validation->set_rules('username', '"username"',
'trim|required|min_length[5]|max_length[52]|alpha_dash|encode_php_tags|xss_clean');
$this->form_validation->set_rules('email', '"email"',
'trim|required|min_length[5]|max_length[52]|encode_php_tags|xss_clean');
$this->form_validation->set_rules('password', '"password"',
'required|min_length[5]|max_length[52]|alpha_dash|encode_php_tags|xss_clean');
if($this->form_validation->run())
{
$this->load->model('users');
if($this->users->register($username,$email,$password) === true)
{
return true ;
}
else
{
echo "Username or email already token" ;
}
}
else
{
echo "please verify your inputs" ;
}
}
}
我真的需要帮助才能继续使用此应用。非常感谢。
答案 0 :(得分:0)
$this->load->library('form_validation');
和网址助手? (对于site_url)if($this->users->register($username,$email,$password))
编辑
编辑2
true
置于CSRF保护(在你的配置文件中)。它在CodeIgniter中与AJAX不兼容,因此请在配置文件中禁用CSRF值。它应该再次工作。 (还有其他方法可以防止CSRF)