Undefined variable on userdata - php

I'm using codeIgniter to developing a website.
This website has a view sidebar and i call on view header with
<?php $this->load->view('sidebar');,
then in another view i call header by using <?php $this->load->view('header');
On sidebar i'm loading the session variables by
$papel = $this->session->userdata('papel');
$logado = $this->session->userdata('logado');
On sidebar it fits perfectly but in other pages like it's doesn't working anymore.
I'm already tried recalling the session variables and got the same results.
ps.: It's alerady autoloaded

Load session library in autoload:
Open autoload.php file located in application/config/autoload.php
Update $autoload['libraries'] variable
$autoload['libraries'] = array('database', 'email', 'session');

Load session in autoload.php found in config folder.
$autoload['libraries'] = array('database', 'session');
Plus to make a session you have to set it:
$this->session->set_userdata('session_name', 'value of the session');
Then calling it:
$this->session->userdata('session_name');

Related

Unable retrieve session value in view in codeigniter

I have session variable on gear.php view as
$_SESSION['dbRecords'] = $product_number;
but when I go to different view eg. order.php, I am unable to retrieve value using
$product_number = $_SESSION['dbRecords'];
order.php loads on submit.
I am using CodeIgniter 3.
How can I get the value of $_SESSION['dbRecords']?
You should get session data via Codeigniter style
setting data to session
$this->session->set_userdata('dbRecords', $product_number);
and get data from sessio
$dbRecord = $this->session->userdata('dbRecords');
I am not sure, the way you are doing is OK.
i think you are not loaded session library
add session library in autoload
application/config/autoload.php
$autoload['libraries'] = array('session');
OR
load in controller __contruct() function
$this->load->library('session');

How to autoload a custom library in codeigniter

I have created a custom library in my codeigniter application and I need that to be autoloaded.
Is it possible to do that for custom Libraries?
I tried adding the same in autoload.php, but getting error.
Error : Unable to load the requested class: Database,Sessions
Sessions.php is my custom library placed inside applications/library package.
Code : $autoload['libraries'] = array('database,Sessions');
You made a mistake in array elements of auto load.
It should be $autoload['libraries'] = array('database','Sessions');
It should be like below
$autoload['libraries'] = array('database', 'sessions');
Put your file in, application/libraries folder and then add your file name in auto-loader libraries array element, application/config
$autoload['libraries'] = array('yourfilname');

Codeigniter 3 HMVC: unable to load session library

Here is the folder structure in modules library
users->controllers->Users.php
users->config->autoload.php
welcome->controllers->Welcome.php
autoload.php
$autoload = array(
'helper' => array('url', 'form'),
'libraries' => array('session')
);
Welcome.php (located in modules/welcome/Welcome.php)
class Welcome extends MX_Controller {
public function index()
{
//load module
$this->load->module(array('users'));
}
}
I will get the following error :
Unable to locate the specified class: Session.php
Note:
url and form libraries are loaded correctly
In config/autoload.php file put below line
$autoload['libraries'] = array('database','form_validation','session');
Its work fine for me.
please load session in index function
$this->load->library('session');
Add this part in your function

Codeigniter login system HMVC

I just installed Codeigniter HMVC and I really like the way it works but I am new to it. My question is how would I check on login? I know I have to make a login module. And afterwards I went into the core folder and added this to the MX_Loader
function __construct(){
$this->load->helper('url');
redirecht('login');
}
But it doesn't recognise the helper command. Am I doing something wrong? I wanted to check on sessions on this page and if the session doesn't exist I wanted to redirect it to the login module. How am I supposed to do this?
Thanks in advance
for this you have to include / autoload the session library.
Try this following example..
open the "application/config/autoload.php file and set the following.
$autoload['libraries'] = array('session');
$autoload['helpers'] = array('url');
in your login controller ( in which you validating the user account exist in the database), once you are done with the database verification, add the below lines
$session_array = array('admin_id' => "1",'username' => "admin"); // here the "1" and the string "admin" are just for example. modify the array key and value as per your requirement.
$this->session->set_userdata('user_logged_in', $session_array);
redirect('admin/dashboard', 'refresh');
By this line, the session will be added into the application. For evaluation, in your function __construct, add the below lines.
function __construct() {
parent::__construct();
if(!$this->session->userdata('user_logged_in')){
redirect('login');
}
}
hope this will help.

How do I load CodeIgniter helpers in every page?

I have a view, header.php, that gets loaded in various controller methods. It contains my opening html tag, a base tag for my relative links, and some meta tags that I call in every page of my application. Is there way to load the helpers that render the meta tags and base url so that they are available to header.php every time it is loaded without having to include $this->load->helper('html'); and $this->load->helper('url'); every time I $this->load->view('templates/header', $data); in a controller to load header.php?
If you're needing these that often, you should just add those to your helpers autoload:
In /application/config/autoload.php around line 93, change
$autoload['helper'] = array();
to
$autoload['helper'] = array('html', 'url');
Then, they're loaded on every request.
Simple, add them to the autoload file. That way they'll be accessible from any file, and you won't ever have to call those.

Categories