Experts,
i have a little Problem with my sessions. I want to save my login data into the session like this:
checklogin Controller
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
var_dump($this->session->set_userdata($user)); // return NULL? is this correct?
var_dump($this->session->all_userdata()); //return the correct data
Now the Session is saved up to a redirect.
Other Controller
var_dump($this->session->all_userdata()); // return session_id,… and a empty user_data array
I think I have the same Problem with the Shopping Cart Class.
Can anyone help?
I dont know why, but i think my story will help someone who facing the same problem.
My problem is just because I forgot to downgrade my Wamp php version, its php 7.1. Looks like my CI not support php 7.1 or higher. So just a few click to downgrade my php, my problem solved.
I added session_start() in the main index.php file. Worked like a charm afterwards.
try this..
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
$this->session->set_userdata('user',$user); //set session of users with a name user.
to get the session value u can do..
print_r($this->session->userdata('user')); // prints the user session array..
read more about sessions in CI
The set_userdata() function does not have a return value, so it is correct to display null on var_dump().
In order to save custom data to user's session you should check the manual here
The parameter it accepts must be an array.
hmm try add to ci->session and then get data
//assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
//add data to ci session
function test(){
$this->ci->session->set_userdata($user)
}
in the other controller assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
and get the ci user data
$this->ci->session->all_userdata()
I got a similar issue when I want to rerun an old website that was built formerly using CI 3.0.6 on a server running PHP 7.3.
I have set a session in one controller.
$this->session->set_userdata('logged_in', TRUE);
Then, the other controllers that are accessed after that cannot retrieve the information while a new record in the session table in the database is stored.
empty($_SESSION['logged_in']); // true
empty($this->session->logged_in); // true
The solution that works for me is as follows.
Set a higher size for the id column in the CI session table. For example, our session table name is ci_sessions.
ALTER TABLE ci_sessions CHANGE id id varchar(128) NOT NULL;
Replace the current CI system directory with newer version. In my case, it works with the system directory from CI 3.1.10.
In summary, a newer CI version is required to be able to interact well with newer PHP (7.1+). It has a longer id value for the stored session record. Rather than downgrading your PHP version, it is better to upgrade the CI version.
Related
I'm still new to PHP and Code Igniter, and developping a project based on a triple store + a small SQLite db.
I want to save some session data in persistent storage when a session is expiring, so that the users could customize the pages and keep their settings and history for new sessions. This seems more practical than using my triple store or db for each user action.
I use code igniter 3.1.5 + Session library + a small SQLite db for session data (followed CI doc for Session library with database driver : https://www.codeigniter.com/user_guide/libraries/sessions.html#database-driver and this post for SQLite settings : How can I create a CRUD with Codeigniter using SQLite?).
I didn't find any solution to retore a closed session (maybe it's possible?), so found that I could either (a) modify CI Session system library or (b) create a custom class that implements SessionHandlerInterface (cf. here : https://secure.php.net/manual/en/function.session-set-save-handler.php ;
and here : How do I save session data to a database instead of in the file system?). Both solutions consist in adding custom code in close() or destroy() function.
I'd like to keep Code Igniter Session library, because it's very simple to use and have already written some code for it, but I don't know how to extend or modify it properly.
So here is what I tried, could you please tell me if it's ok?
1) create a file Session_custom_driver.php in system/libraries/Session/drivers where I copy CI_Session_database_driver code, and modify close() (or destroy()) function
2) edit application/config/config.php : $config['sess_driver'] = 'custom';
And next step would be to load a custom library or model from this modified close() function :
$CI =& get_instance();
$this->CI_Instance =& $CI; <--- add this in __construct()
and
public function close() {
$this->CI_Instance->load->model("MyModelForSessions");
...
}
Thanks in advance.
EDIT : with this solution I can save session data in another place and use it when the session has expired, BUT the function is called every time I modify the data stored in $_SESSION.
How can I save the data just once, i.e. when the session is expiring?
Another idea : I can save session id with user's persistent data, then retrieve the session data corresponding to this id when opening a new session.
Using ajax function working with sessions. In controller with ajax action i'm setting the session values using $this->session->set('coupons',$data). After setting this session i'm not able to get in another controller file already added session.Can you pls advice me.
public function action_applypcode()
{
$this->auto_render = false;
$this->session->set('coupon_details', $restcode);
}
public function action_receipt()
{
$coupon_details = $this->session->get('coupon_details');
print_r($coupon_details);
//Here getting empty session values
}
What is $restcode?
You shouldn't have any problem on setting session values through ajax or normal request, they work equally, the only diff is that you don't want the layout form ajax calls.
Are you using another lib or module that could initialize a session too? Search in your external modules for $_SESSION, sometimes that can be the problem. If that's the problem, try using Session::instance() at the first line of your template controller.
Btw, I don't remember kohana having an attr for the session in the controllers, also you can try using Session::instance()->set and ->get , maybe that could help too.
Regards!
I have simulatneous AJAX requests on codeIgniter with all of them session update like so:
function ajax1($value)
{
$this->session->set_userdata('foo', $value);
}
function ajax2($value)
{
$this->session->set_userdata('bar', $value);
}
But sometimes because of MySQL concurrency, one variable or the other is not updated, I suppose because one method gets overwrites the new value of the other method with the old value if grabbed from the db.
I cannot update the 2 sessions at the same time as they do completely different things and I don't know which ones will be called, as the page is dynamic and might have one or several of these method calls.
Anybody ran into this in the past and has a way of going around that problem?
I think this problem was fixed in the latest version of codeIgniter, but if you are still using an old one, try to replace system/libraries/Session.php with a new one, or you can just override the Session Library like follow:
application/libraries/MY_Session.php
class MY_Session extends CI_Session
{
function sess_update()
{
if (!$this->CI->input->is_ajax_request())
return parent::sess_update();
}
}
I need to get session_id() in ZF2? Using session_id() won't return any value. I tried Zend_Session::getId() but it didn't work, probably because I haven't included the path (which I don't know).
Can anyone help me to find a solution. Thanks in advance.
session_id() should work, but the correct way would be to access it from the session manager, e.g. (from a controller):
$sessionManager = $this->getServiceLocator()->get('Zend\Session\SessionManager');
$id = $sessionManager->getId();
If neither of these are returning a value for you then there's another problem somewhere.
first of all you have to use zend session container in your page that is.
use Zend\Session\Container;
now create an object of container by following.
$session = new Container('User');
now set value $userid or any other variable in session by following object.
$session->offsetSet('userId', $id);
now you can get it by following code.
$user_id=$session->offsetGet('userId');
On every page I see that new session is generated with null userdata
On model constructor
$this->config->set_item('sess_table_name', 'xx_sessions');
Because I want to store this session in another table because the other session table is being used for another login activity
Login function
function login($username,$password)
{
$this->db->where('login',$username);
$this->db->where('pass',$password);
$q=$this->db->get('prof');
// print $this->db->last_query();
if($this->db->count_all_results())
{
$arr=$q->row();
// creating the session
$this->session->set_userdata('login',$arr->id);
$this->session->set_userdata('prof',$arr->profile_id);
// print_r( $arr);
}
else
return FALSE;
}
This login function is on a model. After login and generating the session the page redirects to another page, on that page I see the session builds without any problem but when I move to another page the session losses along with the userdata.
I use the following function to check session data
function print_session()
{
print_r( $this->session->all_userdata());
}
Where I'm wrong ? Tank_auth library and ion_auth library works fine .. I had already used the
Put the session library name into the autoloader configuration, in application/config/autoload.php:
$autoload['libraries'] = array('session');
Then it's available automatically in each controller and everywhere in your application and you get your session data from anywhere:
$session_id = $this->session->userdata('session_id');
And if you don't want to auto load session library then you have to initialize the Session class manually in your controller constructor, use the $this->load->library function:
$this->load->library('session');
For details have a look here:http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Edit /application/config/config.php and set cookie domain variable
$config['cookie_domain'] = ".yourdomain.com";
It will work!
.yourdomain.com makes the cookie available throughout the domain and its sub-domains.
I have met same problem, and i have searched lots of pages.
I figured out that changing sess_cookie_name solves the problem(new sessions generating issue)
$config['sess_cookie_name'] = 'somenewname'