I have created the session in login function. Now I want to use the created session on all other functions too if a user login then the created session should be applied to all other functions.
Thanks
I think you need to understand session first.
How do Sessions work?
Sessions will typically run globally with each page load, so the Session class should either be initialized in your controller constructors, or it can be auto-loaded by the system. For the most part the session class will run unattended in the background, so simply initializing the class will cause it to read, create, and update sessions when necessary.
To initialize the Session class manually in your controller constructor, use the $this->load->library() method:
$this->load->library('session');
Once loaded, the Sessions library object will be available using:
$this->session
Session data is simply an array associated with a particular session ID (cookie).
Visit CI Documentation of Session For More detail
See it live here: Session
In your login section
$this->db->where('email',$email);
$this->db->where('password',$pass);
$query = $this->db->get('admin');
$data= $query->result_array();
if($data){
$this->session->set_userdata('sessionVariable', $data);
redirect('controller_name');
}
Open autoload.php from application/config/autoload.php
$autoload['libraries'] = array('session');
OR
load session libraries in __construct() of your controller
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
To get session data
$sessionData = $this->session->userdata('sessionVariable');
You can autoload the sessions in config.php
$autoload['libraries'] = array('database','Session','email');
OR
You can create a Base Controller in Core folder and extend all your other controller to that base controller.
Like this
<?php
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$this->data['errors'] = array();
$this->data['site_name'] = config_item('site_name');
$this->load->library('session');
}
}
Now All Your other Controller should be extended to your Base Controller instead of CI_Controller
In Your Controller
Controller 1:
class Login extends MY_Controller
{
function __construct() {
parent::__construct();
}
}
Controller 2:
class Dashboard extends MY_Controller
{
function __construct() {
parent::__construct();
}
}
So You will get just need to load your library is your base controller and get all the goodness of base controller in the child controllers. This will give you better hierarchy, code management and security
I am trying extending CodeIgniter controller in my application using composer but it's not working.
This give me
Fatal error: Class 'CI_Controller' not found in D:\xampp\htdocs\ci-dev\application\core\MY_Controller.php on line 11
i knew that if i add spl_autoload_register in my config.php then it is work but i want to use composer.
here is my all set up.
i create MY_Controller in my application/core/MY_Controller.php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
}
}
after this i add admin controller in application/libraries/Admin_Controller.php
class Admin_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
and front-end controller in application/libraries/Frontend_Controller.php
class Frontend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
This is my default controller index call
class Welcome extends Frontend_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
i set up my composer like this in config.php
$config['composer_autoload'] = FCPATH.'../vendor/autoload.php';
and composer.json file like this
"autoload": {
"files" : [
"application/core/MY_Controller.php",
"application/libraries/Admin_Controller.php",
"application/libraries/Frontend_Controller.php"
]
},
CodeIgniter loads CI_Controller after your vendor/autoload.php file, and since you're listing them under the "files" option in your composer.json, they're included immediately as opposed to right when you need them.
That's not only what causes the error, but also beats the entire purpose of using an autoloader - if you'd be explicitly listing the includes, you might as well just require_once them.
What's common in CI, is to require or even directly declare your multiple base controller classes from inside MY_Controller.php - then you know they'll be available exactly when you need them.
But if you insist on loading them through Composer, there's a work-around - list system/core/Controller.php under the autoloaded files as well.
This has been asked a few times and I have seen tutorials yet i failed to do it. I basically have abc.com/admin then admin/controllers. What I need is to load some languages, helpers and libraries for admin area.
class MY_Controller extends CI_Controller{
function __construct(){
parent::__construct();
}
}
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
file_put_contents('trial.txt','trial data'); //JUST TO SEE IF THAT EVEN WORKS and it is not writing the file either
$this->lang->load('scripts','english');
}
}
Nothing is loaded in all Admin areas, unless I autoload them in autoload.php. What am I missing?
The browser:
Unable to locate the specified class: Session.php
This is my Controller:
<?php
class Chat extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Chat_model');
}
public function index() {
$this->view_data['chat_id'] = 1;
$this->view_data['student_id'] = $this->session->userdata('student_id');
$this->view_data['page_content'] = 'chat';
$this->load->view('chat');
}
public function ajax_addChatMessage() {
$chat_id = $this->input->post('chat_id');
$student_id = $this->input->post('student_id');
$bericht = $this->input->post('chat_id', TRUE);
$this->Chat_model->addChatMessage($chat_id, $student_id, $bericht);
}
}
When I put my model in comment in the parent::__construct(); // $this->load->model('Chat_model'); the error is gone.
This is my Chat_model:
<?php
class Chat_model extends CI_Controller {
public function Chat_model() {
parent::__construct();
}
public function addChatMessage($chat_id, $student_id, $bericht) {
$query = "INSERT INTO tbl_chatberichten (chat_id, student_id, bericht) VALUES (?,?,?)";
$this->db->query($query, array($chat_id, $student_id, $bericht));
}
}
class Chat_model extends CI_Controller
should be
class Chat_model extends CI_Model
If you use Codeigniter Modular Extensions HMVC this error can occur if you forget to change your class to extend MX_Controller instead of CI_Controller
So in your case you would start your class with:
class Chat extends MX_Controller {}
Instead of:
class Chat extends CI_Controller {}
I get the same error message when I involve the PDF library with the class name Pdf.php and load it via autoload as 'pdf'.
My mistake, the controller that will display my page I also named Pdf.php, and the error message appears ( that's the reason why I found this question :) ). The problem was immediately solved after I replaced the name of my controller with another name.
Add changes into your library configurations in application/config/autoload.php file
$autoload['libraries'] = array('database', 'session');
and in application/config/config.php set the encryption key(any key u like)
$config['encryption_key'] = 'thu23456789#[n,';
If you still get same error then copy
System/library/Session/Session.php to System/library/ folder, then it
should work
My solution:
Preface: If you don't use hooks, this will not be the solution for you.
I had this same issue, after upgrading to v3.0.6, and I definitely had everything setup correctly, as this is an existing site just being upgraded to v3+. My issue boiled down to hooks that I had loading 'pre-controller'. My hooks worked with v 2.0.X of CodeIgniter, but not with v3+.
If you are loading hooks before the session class is loaded, and your hook has a dependency on the session class, that may be your culprit. Try changing any pre-controller hooks to post-controller, or commenting them out completely to see if that fixes your issue. This is all located in application/config/hooks.php.
I encountered with same error. I attached "session" on --->
yourproject/application/config/autoload.php
$autoload['drivers'] = array("session");
Change your load library configuration in application/config/autoload.php file
$autoload['libraries'] = array('database', 'session');
In application/config/config.php set the encryption key(any key u like)
$config['encryption_key'] = 'thu23456789#[n,';
In my case the library name and controller class name was same i.e my controller class name was Ajaxer and also my library class name was Ajaxer. I changed my library class name to Ajaxer_lib and its resolved the issue.
In your Chat_model, try to change class Chat_model extends CI_Controller to class Chat_model extends CI_Model.
If none of the above solution worked, my workaround was:
In my autoload config, I had a library called 'auth'.
however, in my custom controller, somehow it was being messed up because 'auth' wasn't being auto-loaded(I've checked everything). Since I am only using it when logging in, I removed it from my autoload config and load it in my custom controller.
I never had this issue before but suddenly one day it happened.
Using [CodeIgniter v3]
Add this instead of your Chat controller constructor
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Chat_model');
}
This loads the session library to your controller so that you can use the methods.
Model Extends CI_Model Not CI_Controller
class Chat_model extends CI_Model {
.............
}
Modify your library configurations in application/config/autoload.php file
$autoload['libraries'] = array('database','ci_session');
Set the encryption key for your applicatiion in application/config/config.php
$config['encryption_key'] = 'sdjfkdjkfj';
Copy system/libararies/Session/Session.php to application/libraries/
Rename application/libraries/Session.php to CI_Session.php
Now, CI_Session object will be available through below line
$this->ci_session
My codeigniter site autoloads sessions. I have an XML API page that I created but I'm getting a session error because of that autoload. I would prefer not to load sessions on this controller but I don't want to have to load sessions manually on all of my other controllers. Can that be done?
Use a base controller to load the session class rather than autoload.php and have your controllers extend it. More information here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
// application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
}
// you may add additional base controller classes here
You must extend this controller with the ones that you want to have access to the session class, so unfortunately you will have to make some edits to your existing controllers:
class UserController extends MY_Controller {
public function index()
{
// session class is loaded
}
}
Other controllers can continue to extend CI_Controller and the session class won't be loaded.
I use this method for all my CI projects and rarely use the autoloader.php, it allows much more flexibility.