Welcome Guest, Not a member yet? Register   Sign In
Hiding some pages
#1

[eluser]blaff[/eluser]
Hi,

Some pages on my homepage are only for members. If you not are a member you should return to the first page.

Can I make this simbple some how? if would like to skip to write an if question all the time.

Best regards,
Mikael
#2

[eluser]xwero[/eluser]
you can extend the url helper with this function
Code:
function logged_in($redirect='')
{
    $CI =& get_instance();
    if( ! $CI->session->userdata('logged_in_value'))
    {
       redirect($redirect);
    }
}
In your controllers you only have to add the function with optional redirect segment(s). If you want several methods redirected to one particular method you can add it as a class variable in your controller and then you do
Code:
logged_in($this->first_page);

This function is based on a session variable that is set after a member logs in but you can make different functions with other checks for instance to show pages only to superadmins, editors, ...
It uses the redirect function from the url helper that is why i advised you to add it to the extended url helper file.

It's a very basic way to protect method but you can use it also to protect controllers if you want.
#3

[eluser]blaff[/eluser]
With that function i have to add logged_in() in every function? I would like to have an array where is tell what pages that is avaible for everybody or not in one place.
#4

[eluser]xwero[/eluser]
It can be done but then you have to use a hook (again Wink ) of modify the codeiginter/CodeIgniter.php file.
#5

[eluser]Rick Jolly[/eluser]
Consider extending the controller class. Every controller that should only be available to members could extend a "Members" controller which would extend Controller. The "Members" parent controller could check for a logged in user in its constructor and redirect if the user isn't logged in.
Code:
// Parent class that all members controller would inherit from
class MemberParent extends Controller
{
  function __construct()
  {
    parent::Controller();
    if( ! $this->session->userdata('logged_in_value'))
    {
       redirect('login_controller');
    }
  }
}
Code:
require_once(APPPATH . '/controllers/MemberParent.php');
// This class is accessible to members only
class MembersOnly extends MemberParent
{
  function __construct()
  {
    parent::__construct();
  }
  ...
}
#6

[eluser]Pascal Kriete[/eluser]
A little addition to Rick's solution.

If you place the MemberParent class in a library file called MY_Controller it will be included automatically.




Theme © iAndrew 2016 - Forum software by © MyBB