0
votes

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 :/

2
Did u try to open ur php on browser - Sachin Bahukhandi
@SachinBahukhandi Hi! mm, I think so, but didn't work... I think it has to do with the paths more than code, but can't seem to realize where the problem is :( - xragdollqueen
As per my opinion remove whitespace from ajax url second data type is equal json. If not working than inform me. - Rushabh Shah
I'm sure ur having some issues in ur controller - Sachin Bahukhandi
Ajax url will be ` url: "<?php echo site_url(); ?>login/enter"` same for the form action if needed - Deep 3015

2 Answers

0
votes

in your controller, you are not printing the data, just assigning the data in a variable.

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";
        }
      }
   }

change it to below,

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";
            }
          }
          else 
          {  
              $rta="ERROR";
          }

          echo $rta;

       }

and also your url formation is wrong from your error,

POST http://localhost/myFolder/index.php/%3C?php%20echo%20site_url();%20?%3E.%27index.php/login/enter/%27;?%3E 404 (Not Found)

make sure that you config the following variables in the config.php file like this $config['index_page'] = ''; and $config['base_url'] = 'http://localhost/Yourfolder/';

and your default controller as $route['default_controller'] = 'yourcontrollername'; // not yourcontrollername.php

and if .htaccess file is not created inside your project folder where the index.php file is located, Note : not in your application folder, create a the file with name .htaccess and put the following data

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

and now your url in your script is wrong

url: "<?php echo site_url(); ?>.'index.php/login/enter/';?  

change it to

url: "<?php echo base_url(); ?>login/enter/",  

and try now it helps :)

0
votes

I had the same issue, just found a way, I hope it helps you as well, even tho is a little late. Consider this is the url I want "localhost/proyecto/index.php/iniciocontroller/validarUsuario"

$.ajax(
        {
            type:"post",
            url: "validarUsuario", // just the action name
            dataType: 'json', 
            data: { "rut": rut },
            success:function(data)
            {
                alert(data);
            },
            error: function() 
            {
                alert("Invalide!");
            }
        }
    );