1

I've added a constructor in a class to check if some user is logged in.

public function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->library('encryption');
    if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
        redirect('account/login');
    }
}

Here's the ajax

        $.ajax({
            type:'POST',
            url:'<?php echo base_url("cart/addToCart"); ?>',
            dataType: 'text',
            data:{'id':val},
            success:function(data) {
              if (data != 'added') {
                alert('Opps! something went wrong, please try again');
              }
            }
        });

but if I try to call this request without session, it's not redirecting to login page but giving the whole login page code which I can see in network tab

1
  • you can't do ajax redirection through the server-side. Commented May 14, 2020 at 4:53

3 Answers 3

1

As I know, Ajax simply return a response text. It will not proccess the request

You can try something like following

AJAX

$.ajax({
            type:'POST',
            url:'<?php echo base_url("cart/addToCart"); ?>',
            dataType: 'text',
            data:{'id':val, 'type':'ajax'}, //say this request coming from a ajax
            success:function(data) {
              if (data != 'added') {
                 if(data == "not_log_in"){ //checking whether login or not
                       window.location = "login_page";
                 }else{
                       alert('Opps! something went wrong, please try again');
                 }
              }
            }
        });

Controller

if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
       if(isset($_POST["type"])){ // checking whether request coming from ajax or not
           echo "not_log_in"; // if it is ajax, simply show an error massage
       }else{
            redirect('account/login'); //otherwise redirect
       }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add return to your redirect:

return redirect('account/login');

3 Comments

@fraggely I've added return before redirect(), it's still not redirecting
are you sure you are getting through the if statement? Change the if statement to something that will 100% be true (or remove it completely) to test the redirect. If that works then the issue is your if statement
I've removed if statement in the constructor but it still not redirecting, I can't understand where I messed up
0

I think the problem arises due to ajax request not being completed. When the redirection happens it continues to treat it as ajax request, hence it shows the new page in the network tab.
To solve this you'll have to redirect it to any other function and send a response from it but as you are checking the session in constructor, we'll have to check a condition for that particular function(method).

AJAX

$.ajax({
    type:'POST',
    url:'<?php echo base_url("cart/addToCart/some_new_function"); ?>',  // make a new function {some_new_function}
    dataType: 'json',  // expects json to be returned
    data:{'id':val},
    success:function(data) {
        if (data == 1) { // fail
            window.location.href = "<?php echo base_url('account/login'); ?>"; // your-location-&-logic
        }else{
            // whatever-you-want-to-do
        }
    }
});

Controller

public function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->library('encryption');

    $method = $this->router->fetch_method(); // get method name

    if($method !== 'some_new_function'){     
        if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
            redirect('account/login');
        }
    }
}

function some_new_function(){  // control will come here from ajax after construct
    //perform session check here
     if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
        echo 0; // fail
    }else{
        echo 1; // success // perform-your-task-here
    }
}

Hope this helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.