Welcome Guest, Not a member yet? Register   Sign In
Kind of a Redirect Problem - How to Stop Process not to go further
#1

[eluser]barisv[/eluser]
Hi,

Basically, I am trying to check if the user has been logged in or not. If it is logged in, it should go to members area, if not it should go to another page that is saying "you dont have access to this page, please log in first" .

But the code as below doesnt working properly. If the user was not logged in, it concatenates both pages. I mean, first it opens restricted page, and then concatenates it with members_area' page. The die() method didnt work even if I run it in the if statement. How to handle this problem ?

Thanks in advance.

Code:
<?php

class Site extends CI_Controller{
    
    function __construct()
    {
        parent::__construct();
        
        $this->is_logged_in();
    }
    
    function is_logged_in()
    {
        $is_logged_in =  $this->session->userdata('is_logged_in');
        
        if(!isset($is_logged_in) || $is_logged_in != TRUE)
        {
            $data['main_content'] = 'restricted_page';
            $this->load->view('includes/template.php',$data);

            // how to stop process here. because it goes to run the function of members_area() too.
        }
    }
    
    function members_area()
    {
        $this->load->view('members_area');        
    }
    
    
}


?>
#2

[eluser]SPeed_FANat1c[/eluser]
Quote:function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');

if(!isset($is_logged_in) || $is_logged_in != TRUE)
{
$data['main_content'] = 'restricted_page';
$this->load->view('includes/template.php',$data);

// how to stop process here. because it goes to run the function of members_area() too.
}
}

I have no idea how can it run function members_area if you are not calling it. Try to put comments maybe on the function members_area and check what happens then - if it runs that function now, it should not see that function after you put it in commens and show some error.

And where are your redirects? I can't see them there.
#3

[eluser]barisv[/eluser]
Actually the user tries to call members_area function, but the call should be interrupt by the constructor because user wasnt logged in. Now, while I typing this comment, I got the answer (: It keeps recalling the function so when I put a redirect in if statement, it didnt work because of the constructor. So I call a function from a different controller not the controller itself, and it made redirecting for me.

Thanks you very much.

Code:
function is_logged_in()
    {
        $is_logged_in =  $this->session->userdata('is_logged_in');
        
        if(!isset($is_logged_in) || $is_logged_in != TRUE)
        {
            redirect('login/restrict');
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB