Welcome Guest, Not a member yet? Register   Sign In
Checking to see if logged in when accessing Members Area
#1

[eluser]JamesTaylor[/eluser]
I am trying to implement a members only area and need to have a check which is run every time a page from the members area is loaded.

This is the code i have got so far:

Code:
<?php

class MembersArea extends MY_Controller {

//Makes check for logged in run each time
    function __construct(){
    parent::MY_controller();    
    $this->is_logged_in();
    }

//Check Session data to see if logged in    
    function is_logged_in()
    {
    $is_logged_in = $this->session->userdata('is_logged_in');
    
    if(!isset($is_logged_in) || $is_logged_in !=true)
        {
            $this->login();
        }
    }
    
//validate Login
    function ValidateLogIn(){
    parent::MY_Controller();
    
    $this->load->model('LogIn');
    $query = $this->LogIn->ValidateLogin();

    if($query) // if user's details validated...
    {
    $SessionData = array(
        'MemberEmail' => $this->input->post('MemberEmail'),
        'is_logged_in' => true
        );
        
        $this->session->set_userdata($SessionData);
        redirect('membersarea/index');
    }
    
    else
        {
         $this->login();
        }
    }







//Mainpage
    function index(){
    parent::MY_Controller();
        
    $this->data['main_content'] = 'MembersArea';
    $this->data['title'] = 'Welcome to Bingley St Ives Golf Club - West Yorkshire';
    $this->data['h1'] = 'Bingley St Ives Golf Club - West Yorkshire';
    $this->data['h2'] = 'Bingley St Ives Golf Club Members Area';
    $this->data['BodyClass'] = 'MembersArea';
    $this->data['FlashFile'] = 'crossfade_xml';
    $this->data['Image1'] = 'Image1.jpg';
    $this->data['Alt1'] = 'Pict1.jpg';
    $this->data['Image2'] = 'Image1.jpg';
    $this->data['Alt2'] = 'Pict2.jpg';
    $this->data['Image3'] = 'Image1.jpg';
    $this->data['Alt3'] = 'Pict3.jpg';
    $this->data['Image4'] = 'Image1.jpg';
    $this->data['Alt4'] = 'Pict4.jpg';


    $this->load->view('template', $this->data);
    }
    
    
//LogIn Page
    function login(){
    parent::MY_Controller();
        
    $this->data['main_content'] = 'LogIn';
    $this->data['title'] = 'Golf Club - West Yorkshire';
    $this->data['h1'] = 'Golf Club - West Yorkshire';
    $this->data['h2'] = 'Golf Club Members Area';
    $this->data['BodyClass'] = 'MembersArea';
    $this->data['FlashFile'] = 'crossfade_xml';
    $this->data['Image1'] = 'Image1.jpg';
    $this->data['Alt1'] = 'Pict1.jpg';
    $this->data['Image2'] = 'Image1.jpg';
    $this->data['Alt2'] = 'Pict2.jpg';
    $this->data['Image3'] = 'Image1.jpg';
    $this->data['Alt3'] = 'Pict3.jpg';
    $this->data['Image4'] = 'Image1.jpg';
    $this->data['Alt4'] = 'Pict4.jpg';

    $this->load->view('template', $this->data);
    }
}

It doesn't quite work as intended at the moment but i don't think i'm i'm a million miles off!

What i am trying to achieve is that the 'is_logged_in' function should run on every page load (all pages that are seen behind the mebers area will be functions of this one controller). If the session variable 'is_logged_in' is set to anything but true the user should be redirected to the login page, otherwise they should be able to see the restricted page.

At the moment if 'is_logged_in' returns false it loads the login page - as it should - but it also runs the index function and loads the MembersArea page directly below the login page - so i end with 2 pages loaded on the screen, one below the other?

anyone willing to offer me some advice?

James
#2

[eluser]mattpointblank[/eluser]
In my admin sections, I just put something like this at the top of every restricted method:

Code:
if(!$this->session->userdata('logged_in')) {
     redirect('user/login');
}

Simple and works.
#3

[eluser]JamesTaylor[/eluser]
Matt,

your code would need to be included in each function of the controller though wouldn't it?

I want to try and create it so that it only needs declaring once in the controller and is always run when any of the function within the controller are called... although the number of pages contained within the members area isn't massive it would soon become in-effecient to add the code to each function as a site grows, i'm also going to have add a similar 'logged in' check to an admin backend section for the site.
#4

[eluser]JamesTaylor[/eluser]
Got it sorted!

Needed to use a redirect rarther than $this->function, but that caused a continued looping redirect causing a browser error.

I have moved the Login page and the validation function to its own controller so now when the members area is called and session logged in is not present it redirects away from the Members area controller to the Login Controller and breaks the loop. Seems to work!
#5

[eluser]whitey5759[/eluser]
[quote author="mattpointblank" date="1257808679"]In my admin sections, I just put something like this at the top of every restricted method:

Code:
if(!$this->session->userdata('logged_in')) {
     redirect('user/login');
}

Simple and works.[/quote]

I use a similar approach however I create an extension of the Controller class, and therefore all Controllers you create that extend this will automatically get this functionality, and you don't have to copy-and-paste.
#6

[eluser]w0bbes[/eluser]
I use the same approach as the op but i would like to know how your approach works whitey5759...

Could you post that "extension"
#7

[eluser]Buso[/eluser]
[quote author="w0bbes" date="1258059686"]I use the same approach as the op but i would like to know how your approach works whitey5759...

Could you post that "extension"[/quote]
just make a controller with those 2 lines (name it Main or something like that), and your other controllers instead of begining like this:
Code:
class Bla extends Controller

Make them begin like this
Code:
class Bla extends Main

that way they will inherit those 2 lines
#8

[eluser]whitey5759[/eluser]
[quote author="w0bbes" date="1258059686"]I use the same approach as the op but i would like to know how your approach works whitey5759...

Could you post that "extension"[/quote]

Yeah sure. Couple of quick points:
* You only have to call parent::Controller() or parent::MY_Controller() is the constructor of a Controller which extends either Controller or MY_Controller respectively. You don't need it in your index() or login() functions, or anywhere else for that matter.
* I personally like to have a separate Controller and View for the login stuff, so I wouldn't have it in MembersArea. I think it just makes it too messy and more complicated than it needs to be. In fact, you'd have to detect what Controller and function is being called in the MY_Controller constructor, to make sure that if they are trying to view membersarea/login, then don't do the logic check first. That's kind of messy, so I've not done that in the below example.

Code:
//libraries/MY_Controller.php
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
      
        //If the user is not logged in then proceed no further!
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            redirect("login", "location");
        }
      }
}

//controllers/membersarea.php
class MembersArea extends MY_Controller
{
    function MembersArea()
    {
        parent::MY_Controller();
    }
    
    function index()
    {
        //This won't get called if the login check in the MY_Controller constructor fails.
        //Instead the user will be taken straight to the login screen.
    }
}

//controllers/login.php
class Login extends Controller
{
    function Login()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['main_content'] = 'LogIn';
        $data['title'] = 'Golf Club - West Yorkshire';
        $data['h1'] = 'Golf Club - West Yorkshire';
        $data['h2'] = 'Golf Club Members Area';
        $data['BodyClass'] = 'MembersArea';
        $data['FlashFile'] = 'crossfade_xml';
        $data['Image1'] = 'Image1.jpg';
        $data['Alt1'] = 'Pict1.jpg';
        $data['Image2'] = 'Image1.jpg';
        $data['Alt2'] = 'Pict2.jpg';
        $data['Image3'] = 'Image1.jpg';
        $data['Alt3'] = 'Pict3.jpg';
        $data['Image4'] = 'Image1.jpg';
        $data['Alt4'] = 'Pict4.jpg';

        $this->load->view('template', $data);    
    }
    
    function validateLogIn()
    {
        //.......
    }
}
#9

[eluser]w0bbes[/eluser]
Nice and clear, thanks for the pointers Smile




Theme © iAndrew 2016 - Forum software by © MyBB