Error Login In My HMVC Code Igniter - codeigniter

i have a problem with my login in application . in my login authentication a create a library name is "auth" and i also was call "auth" in autoload but when i call a function in "auth" the error say
Message: Undefined property: Auth::$session
here is my library "auth"
class Auth {
public function cek_auth()
{
$this->ci =& get_instance();
$this->sesi = $this->ci->session->userdata('isLogin');
$this->hak = $this->ci->session->userdata('stat');
if($this->sesi != TRUE){
$this->session->set_flashdata("pesan", "Sesi Login anda telah habis");
redirect('user/login');
exit();
}
}
public function hak_akses($kecuali="")
{
if($this->hak==$kecuali){
echo "<script>alert('Anda tidak berhak mengakses halaman ini!');</script>";
redirect('dashboard');
}elseif ($this->hak=="") {
echo "<script>alert('Silahkan Login!');</script>";
redirect('c_login','refresh');
}else{
}
}
}
my controller :
class Dashboard extends CI_Controller{
function __construct()
{
parent::__construct();
$this->auth->cek_auth(); //ngambil auth dari library
}
function index()
{
$hak_akses = $this->session->userdata('lvl');
if($hak_akses==1) {
$data['content'] = 'dashboard';
return $this->load->view('theme/index', $data);
}
}
}
and my autoload :
$autoload['libraries'] = array('database','form_validation','session','auth','parser');
please give me a best solution , thanks

If you are using session in your code then make sure to autoload in config.php file by setting an encryption key. If you don't autoload it then you can load it using $this->load->library('session'). But you must set encryption key in order to autoload or load session in controller functions.
Reference : Undefined property: CI::$session.
Hope this helps :)

Try loading the session library inside
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('session');
$this->sesi = $this->ci->session->userdata('isLogin');
}

Related

Autoloading form_validation library

I am having problem autoloading the form_validation library in Codeigniter. I have a controller Posts with a create function which works great.
public function create(){
$data['title']='New Post';
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
Now I want to do form validation . Something like this :
public function create(){
$data['title']='New Post';
$this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
$this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
if($this->form_validation->run==FALSE){
$data['errors']=validation_errors();
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}else {
$this->load->view('templates/header');
$this->load->view('posts/success', $data);
$this->load->view('templates/footer');
}
}
For now I am not calling any model to store the data, just showing the success message by loading posts/success view . However , even before I code the create function with the validation(that is the above code) , the moment I add form_validation in the autoload.php (even with the first code) like so :
$autoload['libraries'] = array('database','form_validation');
I am getting the below error :
Message: Undefined property: Post_model::$load
Filename: libraries/Form_validation.php
Line Number: 147
Backtrace:
File: C:\xampp\htdocs\ciblog\index.php Line: 292 Function:
require_once
I do not understand the error as I am not even using the Post_model in the method .
My Post_model.php is :
class Post_model extends CI_Controller {
public function get_post($slug=NULL){
if(!$slug) {
$query = $this->db->get('posts');
return $query->result();
} else {
$this->db->where('slug',$slug);
$query = $this->db->get("posts");
return $query->result();
}
}
}
My complete post controller is this :
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$posts=$this->post_model->get_post();
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($slug){
$posts=$this->post_model->get_post($slug);
if(empty($posts)){
show_404();
} else {
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('/templates/footer');
}
}
public function create(){
$data['title']='New Post';
$this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
$this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
if($this->form_validation->run()==FALSE){
$data['errors']=validation_errors();
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}else {
$this->load->view('templates/header');
$this->load->view('posts/success', $data);
$this->load->view('templates/footer');
}
}
}
I have autoladed the model which is working .I searched other posts , most were to do with the first letter of the model name being not in caps which is not my case . Could somebody please make me understand whats going wrong ?
You used your model in your controller without loading it, you have to load it first:
$this->load->model('post_model');
Another error here is that model extends CI_Model:
class Post_model extends CI_Model
As the documentation says ref you have to validate like this :
I have seen mistake behind the run() method
Please fix it with :
if ($this->form_validation->run() == FALSE)
Also you can check by loading form_validation library directly by updating your constructor
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}

Check for Sessions in Controller _constructor

I am new to Codeigniter and still trying to understand some basics.
I am building a registration/login system. everything good for now.
I am creating a controller to show the user info. Before that I want to check if the session existis and if user is logged in, if not I want to redirect to the site root.
class Myprofile extends CI_Controller {
function __construct()
{
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
The problem is the error I am getting. It looks like I it can't see the sessions in the construct part of the script.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Myprofile::$session
Filename: controllers/myprofile.php
Line Number: 6
This would be the very best solution, otherwise I need to put the same code on all the functions.
Hope to hear some feedback help. Thanks
Perhaps the session library is not loaded. Add session class on the autoload array() located in application/config/autoload.php
eg.
$autoload['libraries'] = array('database', 'session', 'output');
class Myprofile extends CI_Controller {
function __construct()
{
$this->load->library('session');
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}

Code Igniter session declaration

I am new to CodeIgniter framework. I am using 2.1.4 version. I designed a simple login form, with a javascript validation, and the home page of a site. Can you please help me to understand how to declare session , and how to destroy the session on clicking signout link.
controller file of login page ( to load the view page login.php ):-
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index(){
$this->load->view('login');
}
function success() {
redirect ('home');
}
}
The controller file home.php for the view home.php
class Home extends CI_Controller {
// local constructor will be overriding the one in the parent controller class
// for using a constructor in any of my Controllers
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
}
I have designed the view page home.php, and gave the signout link:-
<div class="logout">Signout</div>
For initializing the session, i need to know, what all constructor changes/ config changes need, and the method of session destoy.
To start session library, Go to application/config/config.php and change the below line:
$autoload['libraries'] = array('session');
It would be better if you start your session in the autoload.php. To destroy session you would use :
$this->session->sess_destroy();
To set session :
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
here is an controller... first of all u need to declare a session so that you have two choice to declare one is Go to application/config/config.php change the code as
$autoload['libraries'] = array('session');
and follow this following method (controller)
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
}
function index(){
$this->load->view('login');
}
function success() {
$user=$this->input->post('user');
$psw=$this->input->post('pswd');
$this->load->model('validation');
$result=$this->validation->useraccess($user,$psw);
if($result)
{
$this->session->set_userdata('username', $user); //setting session
redirect ('home');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect('login','refresh');
}
}
this is model where validation done
Class Validation extends CI_Model{
function __construct(){
parent::__construct();
}
function useraccess($user,$pswd)
{
$query = $this->db->query("select * from user where username='$user' AND password='$pswd'");
foreach ($query->result_array() as $row)
{
if($row['username']==$user AND $row['password']==$pswd)
{
return true;
}
else
{
return false;
}
}
}
}
here is a view
login page
create 2 text box and 1 submit button and declare form action as
localhost/index.php/login/success
for logut
localhost/index.php/login/logout

CodeIgniter routing issue, advice how to do it

I'm not CI programmer, just trying to learn it. Maybe this is wrong approach, please advice.
my controller(not in sub directory) :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function process_logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
And a route for login :
$route['user/login'] = 'users/index';
Problem is when I wanna logout, it shows me 404 because I do not have it in my route :
$route['user/process_logout'] = 'users/process_logout';
and in my view I put logout
When I add that, it works, and that is stuppid to add a route for everything. What I'm I doing wrong, please advice.
Thank you
Don't know why you are trying to implement login feature in index() function. However since you said you are learning CI I'm telling something about _remap() function.
Before that. You can try the following routing:
$route['user/:any'] = 'users/$1';
$route['user/login'] = 'users/index';
If you want to take value immediately after controller segment you need to use _remap() function and this function may be solve your routing problem, i mean you don't need to set routing. Lets implement your code controller 'users' using _remap() function.
class Users extends CI_Controller {
private $sections = array('login', 'logout');
function __construct() {
parent::__construct();
}
public function _remap($method)
{
$section = $this->uri->segment(2);
if(in_array($section, $this->sections))
call_user_func_array(array($this, '_'.$section), array());
else show_404(); // Showing 404 error
}
private function _login()
{
$msg = $this->uri->segment(3);
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function _logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}

CodeIgniter Custom Lib

im trying to create a new lib which will auto load each time a controller is loaded and authenticate to see if a user is logged in
I’m auto loading the script and its loading fine how-ever it doesn’t seam to authenticate
My Lib
class Authentication {
var $CI;
function Authenication() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$is_logged_in = $this->CI->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
}
}
}
Any suggestions are greatly appreciated
I see what you are trying to do and I would suggest a different approach.
Create MY_Controller and put it in the core folder. Here is a basic representation of what should be in that file.
class Public_Controller extends CI_Controller
{
// The data array holds all of the information to be displayed in views
public $data = array();
function __construct()
{
parent::__construct();
// Authentication library handles sessions and authentication, etc...
$this->load->library('Authentication');
$this->data['account']['is_logged_in'] = $this->authenication->is_logged_in();
}
}
class Auth_Controller extends Public_Controller
{
function __construct()
{
parent::__construct();
if ( !$this->data['account']['is_logged_in'] )
{
redirect('user/login', 'location');
}
}
}
If you do it this way then for controllers that require authentication you can just extend Auth_Controller otherwise extend Public_Controller.

Resources