CodeIgniter Forums
could not redirect page when no session - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: could not redirect page when no session (/thread-74783.html)



could not redirect page when no session - durairaj - 11-06-2019

//helper function 

function check_session_set() {
   
    $session      = \Config\Services:Confusedession();
    if( $session->has( 'id' ) ) {}
    else {
    echo 'test';
    redirect()->to('public');
    }
    return true;
}



//basecontroller

    public function initController(\CodeIgniter\HTTP\RequestInterface $request,       \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {

    parent::initController($request, $response, $logger);
    helper(['common_helper','form', 'file']);
    $validation = \Config\Services::validation();
    $this->session = \Config\Services:Confusedession();

        check_session_set();

}

when i logout and click back button the page did not redirect to login page.but echo 'test' message shows.
is there redirect work in helper function


RE: could not redirect page when no session - durairaj - 11-07-2019

// home controller
function user() {
if( $this->session->has( 'id' ) ) {
$tmp = view( 'user/index' );
$this->template( $tmp );}else {
return redirect()->to( site_url().'public/login' );
}}

above function is working ....
i want to give this function globally to check session every controller if has session then continue or go to login page.

i tried..
//helper
function check_session_set() {
$session = \Config\Services:Confusedession();
if( $session->has( 'id' ) ) {
} else {
return redirect()->to( site_url().'public/login' );
}
}
//in BaseController

public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
helper(['common_helper','form', 'file','filesystem']);
$validation = \Config\Services::validation();
$this->session = \Config\Services:Confusedession();
check_session_set();
}

this will not redirect page.
there is possible in ci 3.
In ci 4 how to do it