Unable retrieve session value in view in codeigniter - php

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');

Related

Extend $session in another page in laravel

i'm having issue getting session variable in a custom page like a controller.
For example if i create a controller like myController.php and i execute the #dd of data
$data = session::all();
dd($data);
I get no results.
But if i execute the same code in another page like homeController.php i retrieve everyhing the session have like user id etc. My question is how i can retrieve session variable in another page in laravel like i do in plain php with start_session() ?
Check that your route calling this controller you are working in, is under the web middleware, otherwise the session won't be shared.

Undefined variable on userdata

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');

how to pass an array from one class controller to another in codeigniter

I have two Controller such as doc.php, user.php in controller folder and a registration.php in a view folder, which have a registration form.
i want to pass the registration info from registration.php to doc.php controller via user.php.
How can i do this? Need help.
What is your codeigniter version ? 2.x or 3.x ?
Try to use session to save your array.
To initialize the Session class manually in your controller constructor, use this method:
$this->load->library();
Once loaded, the Sessions library object will be available using:
$this->session
You can set you session name, using this code :
$this->session->your_session_name;
Use this to add an item in your session :
$this->session->your_session_name('item_one');
To retrieve your session, use this code :
$this->session->your_session_name;
Check this for CodeIgniter 3.X
Check this for CodeIgniter 2.X
In CodeIgnitor you typically use the session object to pass data between controllers. For example:
// initialise the session object
$this->load->library('session');
// save the array to the session
$this->session->set_userdata('reg_info', $reg_info);
// retrieve the array in the other controller:
$this->session->userdata('reg_info');
More info on CodeIgnitor's session object can be found in the docs.

Where to set session in Codeigniter?

I'm reading the Codeigniter 2.2 tutorial and I am not clear on how to use sessions for logging in.
Say I have a login.php, which checks user data with the database. Then if its ok then I should set the session in a controller?
$this->load->library('session');
And then in say admin.php page I should check if session exists by? :
$this->session->user_data('item'); ??
Or how do I check if the person is logged in?
Thank you
Based on the docs, to do anything custom within session you need to load the session library. If you plan to use session throughout your application, I would recommend autoloading the library. You do this from within config/autoload.php.
$autoload['libraries'] = array('session');
Then you won't have to use $this->load->library('session'); on every page.
After the library is loaded, set your custom information, maybe based off some information from your database. So in your case, this would be in login.php:
$this->session->set_userdata('userId', 'myId'); where userId would be the name of the session variable, and myId would be the value.
Then, on subsequent pages (admin.php), you could check that the value is there.
if($this->session->userdata('userId') == '') { //take them back to signin }
To set user session
$the_session = array("key1" => "value1", "key2" => "value2");
$this -> session -> set_userdata($the_session);
To read user session
$foo = $this -> session -> userdata('key1');
You need $this->load->library('session'); every time prior you use CI session functions. Or you can set it up in autoload.php $autoload['libraries'] = array('session');
load session library
$this->load->library('session');
set session
$_SESSION['email'] = $data['email'];
unset session
$this->session->unset_userdata($_SESSION['email']); // $this->session->sess_destroy();
$this->load->library('session');
$this->session->userdata("email")

Passing data between two controllers MVC

I am using CodeIgniter and I have two different controllers - login.php and site.php
In login.php
function credentials(){
$user = $this->input->post('username');
// there is some code here
}
In site.php, I have function members_area() I need to pass the variable $user from login.php to site.php. How do I do this ?
Thanks
If you are talking about user logins here.. In your Login controller you verify user credentials. If so then you need to set a session variable
$this->sessions->set_userdata('user_id',$user_id);
redirect('site/members_area');
Then in your Site controller you retrieve the data for that user from DB.
$current_user = $this->sessions->userdata('user_id');
And you get your required data from DB.
just take a redirect to site/members_area/$user
and you'll get $user parameter in the function members_area($user)
Turns out that a controller is used to control what is sent to a view. If the design is right you're not really supposed to send data between two controllers.
I sent inserted the value of username into a session.
So, In login.php
$this->session->set_userdata($data);
and retrieved the value in site.php
$data['userid'] = $this->session->userdata('username');
$this->load->view('members_area' , $data); //send the info to the controller
if you want some common functions in two controller put it in a helper file
http://ellislab.com/forums/viewthread/55212/

Categories