Welcome Guest, Not a member yet? Register   Sign In
How to redirect from initController?
#2

(This post was last modified: 03-17-2020, 02:21 PM by captain-sensible.)

im no expert but you had no replies. Is the essence of what you want to do is this:

if somebody is not logged in and tries to access a route they get sent back to home?

if so i have dome something similar . I have CI4 in dev but put it up live ; since i couldn't be bothered to set up SSl certificates for localhost. At https://www.benxmidia.com/usrLogin i have a simple
login page I get their name, password and entered captcha via the form which posts to a route.

All i do if name and password match name and hashed password in database and captcha is correct is set using a session in a controller: $_SESSION['role']="admin";

via a route called newblog
$routes->get('newblog', 'Blog::blogForm');

i display a form that gets the stuff i want to enter.I don't want anybody having access to that route unless they are logged in. I check that using a filter.

public function before(RequestInterface $request)
{
session_start();

$logic=isset($_SESSION['role']) ;
if($logic==false)
{
return redirect('usrLogin');

}


//as you can see if S_SESSION['role'] is either null or not set , any user trying to access my newblog gets brushed off back to login .

the instruction to check is via app/Config/Filters.php using

public $filters = [
'myfilter' => ['before' => ['newblog']],


];


probably i should check that $_SESSION['role'] is set that that it also equals admin, but its early days ,so also have not got around to csrf etc yet either!
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply


Messages In This Thread
How to redirect from initController? - by falko - 03-16-2020, 02:34 PM
RE: How to redirect from initController? - by captain-sensible - 03-17-2020, 02:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB