Welcome Guest, Not a member yet? Register   Sign In
how do I prevent users from accessing the program through url, and have to login before?, I have the login system ready
#1

[eluser]Smalbach[/eluser]
Greetings, I'm new programming in php and codeignier.

I'm working with my application and I have many controllers
example:

www.mysite.com/dashboard
www.mysite.com/proyects
www.mysite.com/proyects/new
www.mysite.com/proyects/edit
www.mysite.com/etc



I need that the user can not access the url,
the user must be logged
some example of how to do it?
#2

[eluser]jojo777[/eluser]
Hi!

Well its pretty common create a function named '_is_logged_in' where you check if the session data 'is_logged_in' or whatever you called it is TRUE. So for example if you dont want people get in www.mysite.com/proyects unless ther are logged do something like this:

Code:
function proyects(){
  if (!$this->_is_logged_in()){
   redirect('users/login', 'refresh');
  }

  #code...
    }

So now lets take a look at what _is_logged_in does. It returns TRUE/FALSE depending on the session data.

Code:
function _is_logged_in(){
     $is_logged_in = $this->session->userdata('is_logged_in');
     if ( ! isset($is_logged_in) || $is_logged_in != TRUE){
            return FALSE;
     }else{
      return TRUE;
     }
    }

Hope it helps you!
#3

[eluser]alexwenzel[/eluser]
If you have a lot of controllers it is better practice to implement the login check within the MY_Controller.php or within codeigniter hooks.

The advantage is, that you can maintain your code more easily and you dont need to add the login cheking method to all your controllers.
#4

[eluser]Smalbach[/eluser]
thanks to respond my answer, alexwenzel, do you have some example to do it?,i have a lot of controller and i do not want to do it in all them...
#5

[eluser]jojo777[/eluser]
You can use the code I posted before, typing it in My_Controller.php
I use it always. Remember to set all your controllers like this

Code:
class anycontroller extends MY_Controller {

}

So from any controller you can call that function like this:

Code:
...
if($this->is_logged_in()){
// good
}else{
// :(
}
...

And depends on this return send users to the correct view.

EDIT: Even you can put that call in your constructor inside My_Controller so always a controler is load it verify if the user is loged in.
#6

[eluser]alexwenzel[/eluser]
#1 method with hooks:

Check out this page to fully understand hooks.

http://ellislab.com/codeigniter/user-gui...hooks.html

My suggested hook type would be: post_controller_constructor

Code:
/**
* Example login hook
*/
class Examplahook {

public function check_login() {

$CI = &get;_instance();

// check if user is logged in

// if NO: redirect them to login
// if YES: do nothing
}

}



#2 method with MY_Controller.php:

Create file application/core/MY_Controller.php

Code:
class MY_Controller extends CI_Controller {

public function __construct()
{
parent::__construct();

// check if user is logged in

// if NO: redirect them to login
// if YES: do nothing
}

}

All you need to do now is, extending all controllers from

All examples above are just typed from out of my mind, dont expect them to work.

But the basic idea behind is often used by myself and should work.
#7

[eluser]Mr. Pickle[/eluser]
Check this site for a simple explaination of how to do this with base controllers:
http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY

It's what @alexwenzel also mentioned with his 2nd option, this link goes further into details and explains also how to seperate admin sections from public sections.
#8

[eluser]Smalbach[/eluser]
thank you very much, is excellent




Theme © iAndrew 2016 - Forum software by © MyBB