I have this code, for a web app with a login. I'm on Windows OS, using CodeIgniter (in Sublime Text 3) along with AJAX calls. I get 403 Forbidden during the AJAX call (for its url).
Right now, I only to enter a username and a password in the login form, take that info and send it through AJAX from a JS file. For now I just want to know if the given user exists in the database, and alert or something about it (it exists / it doesn't exist)
Base_url from config.php:
$config['base_url'] = 'http://localhost/myFolder/';
Here is the login (part of it):
<form action="<?php echo base_url(); ?>index.php/login/enter" method="POST" id="formularioLogin">
<div class="form-group row">
<label for="userInfo" class="col-md-2 col-sm-12 col-form-label">User:</label>
<div class="col-md-10 col-sm-12">
<input type="text" class="form-control" id="userInfo" name="userInfo" placeholder="">
</div>
</div>
<div class="form-group row">
<label for="passwordInfo" class="col-md-2 col- sm-12 col-form-label">Password:</label>
<div class="col-md-10 col-sm-12">
<input type="password" class="form-control" id="passwordInfo" name="passwordInfo" placeholder="">
</div>
</div>
<div class="form-group row text-right">
<div class="col-md-12 col-sm-12">
<button type="button" class="btn btn- primary" id="botonSubmitLogin" onClick="login()">Login</button>
</div>
</div>
</form>
Here's the JS file with the login function: function login(){
if(validacionLogin()){
$.ajax({
url: "<?php echo site_url(); ?>.'index.php/login/enter/';? >",
type: "POST",
data: {"userInfo": userInfo,
"passwordInfo": passwordInfo,
},
dataType: "html",
cache: false,
beforeSend: function() {
console.log("Procesando...");
},
success: function(data) {
if(data == "OK"){
alert("Exists");
console.log(data);
return;
}
if(data == "ERROR"){
alert("Doesn't exist");
console.log(data);
return;
}
}
});
//alert("data passed");
console.log("User: "+userInfo);
console.log("Password: "+passwordInfo);
}else{
alert("Incorrect");
console.log("User: "+userInfo);
console.log("Password: "+passwordInfo);
}
}
Here is the model for the login (login_model):
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login_model extends CI_Model{
public function __construct(){
}
public function login($user, $password){
$this->db->where('user', $user);
$this->db->where('contrasenia', $password);
$query = $this->db->get('users');
if($query->num_rows()>0){
return true;
}else{
return false;
}
}
}
And here is the controller (login.php):
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->model('login_model');
}
function enter(){
$usuario = $_POST['userInfo'];
$password = $_POST['passwordInfo'];
if(isset($user) || isset($password)){
if($this->login_model->login($user, $password)){
$rta='OK';
}
else{
$rta="ERROR";
}
}
}
function index() {
$this->load->view('login.php');
}
}
Note: I've already have a database with users on it, and I've also added the database in autoload.php and database.php
Still, when I press the "Log in" in the form, I get this error in dev tools:
POST http://localhost/myFolder/index.php/%3C?php%20echo%20site_url();%20?%3E.%27index.php/login/enter/%27;?%3E 404 (Not Found)
But depending if I change the default controller to login.php, I get 403 (Forbidden) error. I don't have much idea on how to advance :/
EDIT: I've tried what user @Nobita suggested. Result: error 403 Forbidden. EDIT2: I went back to having the code as I had it (including index.php in config.php and changin the dafult controller to welcome) and hardcoded the link in the ajax url to --> http://localhost/myFolder/index.php/login/enter/ Result: jquery.min.js:6 POST http://localhost/myFolder/index.php/login/enter/ 500 (Internal Server Error) I really don't know what's going on :/