Welcome Guest, Not a member yet? Register   Sign In
How to check if a user logged in?
#1

I can check and restrict access to certain methods or controllers by defining a helper method
PHP Code:
function checkLoggedIn() {
 
   if (session('loggedin') == true) {
 
       return true;
 
   }
 
   header("Location: /login");
 
   exit;


Then an example usage is as below.

PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Home extends Controller
{
 public function 
index()
 {
 return 
view('welcome_message');
 }

 
   public function showme($page 'home')
 
   {
 
       checkLoggedIn();
 
   }
}

class 
AdminHome extends Controller
{
 
   public function __construct()
 
   {
 
       checkLoggedIn();
 
   }

 
   public function index()
 {
 return 
view('welcome_message');
 }


Do you have any better method than this helper method? E.g. in laravel there are guards. Could I achieve something similar without having to write too much of code?
Reply


Messages In This Thread
How to check if a user logged in? - by happyape - 03-05-2019, 06:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB