Welcome Guest, Not a member yet? Register   Sign In
User Authentication - Placement of code?
#9

[eluser]ekeretex[/eluser]
Using a profile page as an example:
In the controller:
Code:
class Account extends Controller {

  public $data;
  
  function profile(){    

    $this->data['session_id'] = $this->session->userdata('session_id');

    //pass the session_id into a view variable (assuming the name is stored there as per your example)
  
    if (empty($this->data['session_id'])) {

      redirect('login');

      exit(); //optional
    }
    //rest of controller code goes here
  }
}
and in the view:
Code:
<p>Welcome &lt;?php echo $session_id; ?&gt;</p>

//rest of your html page here

The difference here is that the view file is not making decisions just displaying data passed to it from the controller.

To apply to all the functions in a class, put in the constructor e.g.
Code:
class Account extends Controller {  
    
    public $data;

    function __construct() {

      parent::__construct();

      $this->data['session_id'] = $this->session->userdata('session_id');

      if (empty($this->data['session_id'])) {

        redirect('login');

        exit(); //optional
      }
    }

    function profile() {  
      //controller code goes here
    }
}

Of course 'login' will have to be in a different class to avoid an infinite loop.


Messages In This Thread
User Authentication - Placement of code? - by El Forum - 01-21-2008, 12:31 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 12:40 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 12:48 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 12:59 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 01:10 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 02:55 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 03:55 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 04:01 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 04:22 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 06:05 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 06:21 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 06:37 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 07:07 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 07:11 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 07:25 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 07:34 PM
User Authentication - Placement of code? - by El Forum - 01-21-2008, 08:01 PM
User Authentication - Placement of code? - by El Forum - 01-23-2008, 03:43 PM
User Authentication - Placement of code? - by El Forum - 01-23-2008, 05:42 PM
User Authentication - Placement of code? - by El Forum - 01-25-2008, 03:55 AM
User Authentication - Placement of code? - by El Forum - 01-25-2008, 08:58 AM
User Authentication - Placement of code? - by El Forum - 01-25-2008, 09:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB