0

I am using Codeigniter to build my project.

Here i have some doubts or need some clarification.

Can i use Constructors to do somethings that which affect all other functions in codeigniter/php ??

Please take a look here :

<?php
class New extends CI_Controller
{
 function __construct()

{

//Constructor codes...

}
 function Create_page()  //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied. 
}
 function Edit_page()      //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied.
}
function Delete_page()  //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied.
}
function about_us()   //This is a public action no need to Log in 
{
   // this is a pulic action ,no need to check the login status
}
}
?> 

As you can see that i need to check the Logged in status on each Private functions ,

Is there any way that i can do this on constructor ? so that constructor will check logged in or not ....but it will only need to affect some Functions...

4
  • there are all public functions Commented Dec 15, 2011 at 13:03
  • oh ... what is logical ? public = private ? (that's what you mean is logical?) Commented Dec 15, 2011 at 13:14
  • Shouldn't you avoid using a reserved keyword ("New") as a class name? Commented Dec 16, 2011 at 2:21
  • @landons This is just a example only,please dont mind the class name. Commented Dec 16, 2011 at 4:03

5 Answers 5

3

Call a function in constructor, which checks following steps:

  • define which methods needs login, which do not.
  • detect the current method call.
  • if required, then do login, else not.

<?php
class New extends CI_Controller
{

  var $publicMethods  = array("aboutUs"); 

 function __construct()

{

//Constructor codes...
  $this->_validateLogin();

}

 function _validateLogin()
 {
    $router = &load_class('Router');
    $currentMethod = $router->fetch_method();
    if(in_array($currentMethod,$this->publicMethods) == false){
       // call some login functionality
    }
  }

 function Create_page()  //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied. 
}
 function Edit_page()      //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied.
}
function Delete_page()  //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied.
}
function about_us()   //This is a public action no need to Log in 
{
   // this is a pulic action ,no need to check the login status
}
}
?> 
Sign up to request clarification or add additional context in comments.

Comments

2

I solved this issue in the following way:

  1. I made a new core class called "MY_Controller" which extends the CI controller class.
  2. I wrote a doAuth() method which authenticates user or reroutes him to a login controller.
  3. Now I call this method from all methods witch must be safe

It seems a lot less practical than other aproaches, but it saves you some work if you decide you need another controller to feature authentication.

1 Comment

This is the most practical approach. In fact, you can take it further by having one controller inheriting from CI_Controller that checks for authentication and another that inherits from that to REQUIRE authentication. Then you can always have the authentication data available, and you can create controllers that which all require authentication.
0

To answer your question in a line yes you can do. Create library and add a function as i have done below and load the library within the constructor and make a call to that function. I assume you set a signal when a user login to your site like user_login within the session. Hope this answers your question.

// Place this function in the user library
  check_user_login(){
  $obj = get_instance();
   if($obj->session->userdata('user_logged') != true):
  // redirect to login page
   endif;
 }    
<?php
class New extends CI_Controller
{
 function __construct()

{

//Constructor codes...
//load the library,
$this->load->library("library_name");
// call the function name
check_user_login();

}

 function Create_page()  //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied. 
}
 function Edit_page()      //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied.
}
function Delete_page()  //The user need to be Logged in to perform this
{
 //Checking whether the user logged in or not if yes allowed else denied.
}
function about_us()   //This is a public action no need to Log in 
{
   // this is a pulic action ,no need to check the login status
}
}
?> 

Comments

0

if you give your constructor the password and username and than check if its loggend in you van save it in a var ( true/false )

class newclass{

 public function __construct($username,$password){

  $query = mysql_query("SELECT * FROM accounts WHERE user = '".$username."' AND pass = '".$password."'");
  if(mysql_num_rows($query) > 0){ // if username and password match in database your loggend in
   $this->loginCheck = true;
  }
  else{ // if not match not logged in
   $this->loginCheck = false;
  }
 }//end constructor

 public function another(){
  if($this->loginCheck){
   return "logged in";
  }
  else{
   return "nog logged in";
  }

 }// end another function

}//end class

$class = new newclass($user,$pass);
echo $class->another();

Comments

0

Basically, you should separate those private methods you want to be used by a logined user and the public methods which can be accessed by everyone. And if you really want to do, you can do something like the following methods. Moreover, you can modify the auth_method() to satisfy your needs with more authentications.

class Page extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->auth_method();
}

public function create_page()
{
    // ...
}

public function edit_page()
{
    // ...
}

public function delete_page()
{
    // ...
}

public function about_us()
{
    echo 'About Us Page';
}

private function auth_method()
{
    $protected_methods  = array('create_page', 'edit_page', 'delete_page');
    $segs   = $this->uri->segment_array();
    $method = isset($segs[1]) ? trim($segs[1]) : 'index';
    if(in_array($method, $protected_methods))
        exit("Access Denied.");
}

}
//END CLASS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.