Retrive some data store in session with codeigniter - ajax

I need to store some data that i get via an ajax request on a method, and later retrive them when i call another method.
public function updateInt()
{ $this->load->library('session');
$interval = $this->input->post('_interval');
$aInt = array('my_interval' => $interval);
$this->session->set_userdata('post', $aInt);
$_interval_ = $this->session->userdata['post']['my_interval'];
return $_interval_;
}
public function getInt()
{
$interval = $this->updateInt();
// print $interval and do some stuff !!
}
// This return a null, but I need the value set on the front-end by the user and passed with an ajax call to updateInt() method.
I need some help because I'm newbie with codeignter.

Make sure you have set your sessions in your config.php do not leave the session save path null
Example
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH . 'cache/session/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Make sure session folder permission 0700
Make sure you are getting correct post data what your after
public function updateInt()
{
// You can autoload it in config/autoload.php saves loading every page
$this->load->library('session');
$aInt = array('my_interval' => $this->input->post('_interval'));
$this->session->set_userdata('post', $aInt);
// Use `(` and `)` not `[]`
$interval = $this->session->userdata('post');
return $interval;
}
public function getInt()
{
$interval = $this->updateInt();
// Test
echo $interval['my_interval'];
// print $interval and do some stuff !!
}

Related

Why Flashdata value gets null on redirecting to another function in Codeigniter?

I am trying to get the input values from the view to controller and saving this value as flashdata to use it in the other controller. Now the i am able to store the value until i redirect, but as soon as it comes to redirect, it loses its value. I am not sure whats going over here.
Do I need to see my config again? If yes, then what I need to see?
My both the functions are rights but if I am missing anything?
Here is my code below:-
Controller.php
public function first() {
$testing = $this->input->post('fname');
$this->session->set_flashdata('fstname', $testing);
$this->session->keep_flashdata('fstname');
// echo $this->session->flashdata('fstname'); //able to get the flashdata value till here.
redirect('Home/second/'); //but when I am using this, flashdata loses its value
}
public function second() {
$data['getfname'] = $this->session->flashdata('fstname');
$this->load->view('details', $data);
}
View (details.php)
<?php echo $getfname ?>
Output
Null
config.php
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH . 'cache/sessions/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
autoload.php
$autoload['libraries']=array();
Additionally, I also tried using userdata() but its not showing on the second function after redirect.
Thanks for your time.
First:
check if Post data is not empty
$testing = $this->input->post('fname');
var_dump($testing);
exit;
Second:
you can skip keep_flashdata, you dont need that in that context
Third:
Check session setting in config.php for example:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'mysession';
$config['sess_expiration'] = 86400;
$config['sess_save_path'] = 'sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
check autload.php:
$autoload['libraries'] = array('session', [...]);
Fourth:
check session flash var directly:
$strFstname = $this->session->flashdata('fstname');
die($strFstname);
Your code should work, I used flashdata like this many times.

why in Codeigniter my session lost in other pages?

it's first time I use Codeigniter 3.x. after authentication for login I set session data in Login_model.php:
$data_session = array(
'PersonId' => $PersonId,
'PersonEmail' => trim($PersonEmail),
'PersonName' => trim($PersonName),
'PersonFamily' => trim($PersonFamily),
'Login' => true);
$this->session->set_userdata($data_session);
before redirect I have access to my session data by :
print_r($this->session->userdata());
Array([__ci_last_regenerate] => 1510383844 [PersonId] => 129[PersonEmail]=> h#q.com [PersonName] => PersonName [PersonFamily] => PersonFamily [Login] => 1)
but after redirect my session data is something like this:
Array( [__ci_last_regenerate] => 1510384212)
also my setting in config.php is:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'east';
$config['sess_expiration'] = 7200;
//$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
//------------------
$config['cookie_prefix'] = '';
$config['cookie_domain'] = 'domain/admin/';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
I have reviewed a lot of things in this regard. but I still have not gotten a good result.
when using
$config['sess_driver'] = 'files';
then $config['sess_save_path'] must be set to the absolute path of the folder where session files are stored.
With this folder structure
webroot/
application/
sessions/
system/
...etc/
Then
`$config['sess_save_path'] = FCPATH."sessions/";`
will work perfectly.
Folder write permissions must be set appropriately.
EDIT
You should try setting $config['cookie_domain'] = ''; (an empty string) and if that does not work try $config['cookie_domain'] = '.your-domain.com';
The following controller will allow you to test if sessions are working correctly. Create a file named Test_sessions.php in your controllers folder.
Use this code.
<?php
/**
* This class is useful for testing the configuration of the CodeIgniter "session" library.
* It works only for CodeIgniter versions >= 3.0.0
*
* If "session" is properly configured then each time you ask a browser for this page
* the display will alternate between "No session data" and the value of $this->sess_message.
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Test_sessions extends CI_Controller
{
/**
* #var string So you can define your own message to be displayed.
*/
public $sess_message;
function __construct()
{
parent::__construct();
// The next line can be commented out if 'session' is autoloaded - or not, doesn't matter.
$this->load->library('session');
$this->sess_message = "We got session data!";
}
public function index()
{
echo date("F j, Y, g:i:s a")."<br>"; //so you can tell the page actually does refresh
if($this->session->testing)
{
echo $this->session->testing;
unset($_SESSION['testing']);
}
else
{
echo "No session data";
$_SESSION['testing'] = $this->sess_message;
}
}
}
Browse to http://yoursite.com/test_sessions. If sessions are properly configured, every time you refresh the page it will alternate between the two messages.
I solved my problem by change my config to:
$config['sess_driver'] = 'files';$config['sess_cookie_name'] = 'ci_session';$config['sess_expiration'] = 86400;$config['sess_save_path'] = FCPATH.'sessions/';$config['sess_match_ip'] = FALSE;$config['sess_time_to_update'] = 3600;$config['sess_regenerate_destroy'] = TRUE;$config['cookie_prefix'] = '';$config['cookie_domain'] = '';$config['cookie_path'] = '/';$config['cookie_secure'] = FALSE;$config['cookie_httponly'] = FALSE;

session_id in codeigniter returns nothing

My CI_VERSION is 3.0.6.
now I want to echo session_id, but it returns nothing!
class MainIndex extends BaseController
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->library->load('session');
echo $this->session->userdata('session_id');
return ;
...
my config:
$config['sess_driver'] = 'files';
$config['sess_save_path'] = BASEPATH . 'soheil/files_cache/';
$config['sess_valid_drivers'] = array();
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 68400;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_session';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 68500;
Try to print all session data using.
Also make sure that you have loaded session in config/autoload.php
$autoload['libraries'] = array('session');
print_r($this->session->all_userdata());
also in order to use the session you are require to add some string in $config['encryption_key']
Just having a read in the Codeigniter user guide, it makes mention that session_id is no longer available via the sessions library since "sometime ago" (My Words, not theirs...)
The alternative they recommend is session_id().
Give that a shot!

Codeigniter Session only writes userdata when sess_use_database is FALSE

I'm new to codeigniter and so when I set up my log in code I started out with simple and kept updating it to be more complex/secure. With that said I was making great progress creating a session and adding a user_data variable called "login_status" set to "1". To use as a reference for future page requests. Eventually I decided to go ahead and set up the database table ci_sessions and switch to that instead of just using a cookie. When I did this, all of the sudden my "login_status" variable was not being written anymore. As a result I could no longer access any subsequent pages and kept being redirected back to the log in screen.
In short, this exact same code works perfectly when I have sess_use_database set to false.
I'm not sure why this is happening but any help would be greatly appreciated!
Log in Controller:
class login extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
$this->load->helper('form');
}
public function index($login = "")
{
$data = array();
if ($login == "failed")
$data['loginFailed'] = true;
else
$data['loginFailed'] = false;
$this->load->view('templates/headerAdmin');
$this->load->view('admin/loginform', $data);
$this->load->view('templates/footerAdmin');
}
public function assessme()
{
$username = $_POST['username'];
$password = md5($_POST['password']);
//checkme works fine and returns true
if ($this->checkme($username, $password))
{
$newdata = array( 'login_status' => '1' );
$this->session->set_userdata($newdata);
$this->mainpage();
}
else {$this->index("failed");}
}
public function checkme($username = "", $password = "")
{
if ($username != "" && $password != "")
{
$this->load->model('admin/loginmodel');
if ($this->loginmodel->validateCredentials($username, $password))
return true;
else
return false;
}
else
{
return false;
}
}
public function mainpage()
{
redirect('admin/dashboard');
}
The controller that I am redirected to after I can successfully log in:
class dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
}
public function index() {
//Make sure user is logged in
$login_status = $this->session->userdata('login_status');
//This is where I am redirected because the user_data is not being set
if(!isset($login_status) || $login_status != '1') {
redirect('admin/login');
}
$this->load->view('templates/headerAdmin');
$this->load->view('admin/dashboard/list');
$this->load->view('templates/footerAdmin');
}
}
Config:
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
EDIT : Setting 'sess_match_useragent' to FALSE seems to prevent the session from being destroyed. Hopefully that will provide other clues as to what the cause of my problem is but obviously this, in itself, isn't an ideal solution
I had this problem and I fix it by this page
http://philsbury.co.uk/blog/code-igniter-sessions
This page say u should change Session library in \system\libraries
I Rename default Session file And create new Session file and put the code on it
its Worked

CodeIgniter Session - i am not getting my userdata in other controller

here is my code to set session
$login_data = array('logged_in'=>TRUE, 'user_id'=>$userid);
$this->session->set_userdata($login_data);
redirect('dashboard');
above code creates a new entry in ci_session table in my db and it has logged_in value
In dashboard controller i am checking session. Below is my code in dashboard controller
function __construct(){
parent::__construct();
$logged_in = $this->session->userdata('logged_in');
if(!$logged_in){
redirect("welcome");
}
}
In above constructor block i am not getting the value of logged_in.
I have also tried to print all my userdata using var_dump($this->session->all_userdata) but all I get is an empty string.
Please tell me what could be the reason behing it. I also searched for cookie but i didn't find that in my computer. below is my config detail
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
I have solved my problem. I just forgot to change my Cookie Related Variables in config. –
My first guess is being you're doing this in the __construct method which is executed when the class is initialized - before the page is even loaded and your sessions data is set.
$logged_in = $this->session->userdata('logged_in');
if(!$logged_in){
redirect("welcome");
}
Will work if you add in into the beginning of the page method.
Is the library being autoloaded? Check config/autoload.php:
$autoload['libraries'] = array('database', 'session', 'form_validation');
Or call $this->load->library('session'); right before using the class.

Resources