i have crated a custom library file for the login validation. if i call the custom library at before $this->load->library('form_validation');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('loginuser');
$this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
}
function index()
{
//my code
$this->load->library('form_validation');
}
}
This is the error i got $this->load->library('form_validation'); it works perfectley.
i just want to know Why this code works? Am i overwriting the default libraries?
Message: Undefined property: VerifyLogin::$form_validation
if i load the library after
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('loginuser');
$this->load->library('form_validation');
$this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
}
function index()
{
//my code
}
}
It looks like you're loading your libraries wrong. Try one of these;
Multiple
$this->load->library(array('library1', 'library2'));
Single
$this->load->library('library1');
$this->load->library('library2');
#sobiaholic
this is validate_login
class Validate_login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('loginuser');
$this->load->helper('url');
}
function is_logged()
{
if(isset($this->session->userdata['my_session_id']))
{
if(strlen($this->session->userdata['my_session_id']))
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
function validate_login()
{
$session_id=$this->session->userdata['my_session_id'];
$this->db->select('last_activity,user_data');
$this -> db -> from('sessions');
$this->db->where('session_id',$session_id);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
$results=$query->result();
$active_session=$this->session_alive($results[0]->last_activity,$session_id);
if($active_session==TRUE)
{
return TRUE;
}
else
{
$this->verifylogin->logout();
}
}
else
{
return false;
}
}
function session_alive($valid_till,$session_id)
{
$time_limit=$this->config->item('sess_expiration');
if (time() - $valid_till > $time_limit)
{
return FALSE;
}
else
{
if($this->update_session($session_id)==TRUE)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
function update_session($session_id)
{
$new_time=time();
$data=array('last_activity'=>$new_time);
$this->db->where('session_id',$session_id);
$this->db->update('sessions',$data);
if($this->db->affected_rows())
{
return TRUE;
}
}
}
Related
The value of variable $jwtoken in method addNew() is null. I want to pass auth() method value "some val" to addNew() method/function.
class Employees extends CI_Controller
{
public $jwtoken = null;
public function __construct()
{
parent::__construct();
}
public function auth() {
if(condition is true){
$this->jwtoken = 'val1';
}
else{
$this->jwtoken = 'val2';
}
}
public function addNew()
{
$this->auth();
echo $this->jwtoken;
}
// ..
}
Try this..
class Employees extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function auth() {
if(condition==true){
return "some val";
}else{
return "some other val";
}
}
public function addNew()
{
echo $this->auth();
}
}
Your code is working fine in codeigniter 4. try removing constructor.
I want to declare a global variable with a value and access it in the functions.
I'm done below code but showing Undefined variable: msg1 in the login function.
Please help me to do this.
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->$msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Try this,
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Remove $ from $msg1 in login function
$msg = $this->msg1;
I am gettig a error on my library
Undefined property: Authenticate::$ci
Here is my custom library function
function is_logged_in() {
$sessionid = $this->ci->session->userdata('moderId');
if($sessionid) {
return isset($sessionid);
} else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
Here is my controller
class B2bcategory extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}
I did not see if you had loaded the get_instance() in your construct area of library, Try some thing like this in your authenticate library
Filename: Authenticate.php
<?php
class Authenticate {
public function __construct() {
$this->CI =& get_instance();
}
function is_logged_in() {
$sessionid = $this->CI->session->userdata('moderId');
if($sessionid) {
return isset($sessionid);
} else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
}
I have found that seems to work better with $this->CI on your library with get_instance.
To load library on controller if you have not autoload it use.
Filename: B2bcategory.php
<?php
class B2bcategory extends CI_Controller {
public function __construct() {
parent::__construct();
// Or Auto load it
$this->load->library('authenticate');
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}
$this->load->model('moderator/b2bcategory_model', 'authenticate');
$this->load->model('REAL_MODEL_PATH', 'PROPERTY_IN_CONTROLLER')
USAGE $this->PROPERTY_IN_CONTROLLER->library_method()
but best way is in application/core create ModeratorController.php
ModeratorController extends CI_Controller
{
public function __construct()
{
parent::__construct();
if($this->session->userdata('moder_id') === false)
{
redirect('site/moder_login');
}
}
}
And all moder controllers extend from this controller
<?php
class controller
{
public function view()
{
echo "this is controller->view";
}
}
class home extends controller
{
public function index()
{
echo "this is home->index";
}
function page()
{
echo "this is home-> page";
}
}
$obj= new home;
$method="index";// set to view or page
if(method_exists($obj,$method))
{
$obj->{$method}();
}
?>
my problem :
If we set $method to view, the view() from base controller class will be called.
i want to check if $method exist on home class only
(don't want to check if the function is defined in base class )
any idea how this can be implimented?
Define base class function as private.
Change
public function view()
{
echo "this is controller->view";
}
to
private function view()
{
echo "this is controller->view";
}
It will be work...
EDIT
function parent_method_exists($object,$method)
{
foreach(class_parents($object) as $parent)
{
if(method_exists($parent,$method))
{
return true;
}
}
return false;
}
if(!(method_exists($obj,$method) && parent_method_exists($obj,$method)))
{
$obj->{$method}();
}
This will working perfectly in your case...
Also refer this link
Based off of #vignesh's answer, I needed to use is_callable() to make it work.
abstract class controller {
private function view() {
echo "this is controller->view";
}
}
class home extends controller {
public function index() {
echo "this is home->index";
}
public function page() {
echo "this is home->page";
}
}
$home_controller = new home;
is_callable([ $home_controller, 'view']); // false
I have created a custom model (My_Model) containing all the crud functions. now i want to inherit that general model class in other models.
application/core/My_Model.php
<?php
class My_Model extends CI_Model {
protected $_table;
public function __construct() {
parent::__construct();
$this->load->helper("inflector");
if(!$this->_table){
$this->_table = strtolower(plural(str_replace("_model", "", get_class($this))));
}
}
public function get() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->get($this->_table)->row();
}
public function get_all() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->get($this->_table)->result();
}
public function insert($data) {
$success = $this->db->insert($this->_table, $data);
if($success) {
return $this->db->insert_id();
} else {
return FALSE;
}
}
public function update() {
$args = func_get_args();
if(is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->update($this->_table, $args[1]);
}
public function delete() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->delete($this->_table);
}
}
?>
application/models/user_model.php
<?php
class User_model extends My_Model { }
?>
application/controllers/users.php
<?php
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model("user_model");
}
function index() {
if($this->input->post("signup")) {
$data = array(
"username" => $this->input->post("username"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"fullname" => $this->input->post("fullname")
);
if($this->user_model->insert($data)) {
$this->session->set_flashdata("message", "Success!");
redirect(base_url()."users");
}
}
$this->load->view("user_signup");
}
}
?>
when i load the controller i get an 500 internal server error but
if i uncomment the line in controller -- $this->load->model("user_model");
then the view page loads,...cant figure out whats happening...plz help..
In CI config file 'application/config/config.php' find and set configuration item
$config['subclass_prefix'] = 'My_';
then the CI load_class function will load CI_Model and My_model when calling $ths->load->model('user_model') in your routine;