da1lbi3 da1lbi3 - 7 months ago 21
PHP Question

Redirect whole website to one url

I have multiple views in my website, some code:

$this->load->view('templates/header');
$this->load->view('main');
$this->load->view('templates/footer');


I have an div in the main where I load another view when you click on a button.
The js code:

$("#side").load("/admin/side/loadNewContact", function(e) {
$("#side").removeClass('hide');
});


A separate function checks if the user is stil valid and logged in everytime A request is done.

When the user is not valid the system needs to go to the login page.
I have this code for it:

redirect('/', 'location');


This works. But now the problem.

When I load an page inside the main with the js, and a user is not valid. The system redirects to / (the login page). But the view what you get is loaded in the side, that is not what I want. I want that the whole system is redirected to the login instead of that specific view.

Answer

If I understand problem corectly, you would need if than else that wraps current jQuery code:

//pseudo code
var valid_user = <?php echo $valid_user;//this should be value or FALSE/NULL?>

if (valid_user) {
    $("#side").load("/admin/side/loadNewContact", function(e) {
        $("#side").removeClass('hide');
    });
} else {
    // similar behavior as an HTTP redirect
    //window.location.replace("<?php echo base_url();?>");

    // similar behavior as clicking on a link
    //window.location.href = "/";
}

You want to check JavaScript variable at the beginning of the file/document so you can make similar conditions due the file accordingly. I am saying that in case of buttons/classes need to be shown regarding valid_user state.

Comments