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.
Related
I have buid a project in codeigniter and I set CI session with key USER and value 'ABCD'.
$this->load->library('session');
$this->session->set_userdata('USER','ABCD');
And now I put some native PHP code outside the application directory and from there I have to update the session data having key USER to value 'XYZ' .
I can not change the session used in CI as it is being implemented in many pages. I can only change to native PHP code.
I also tried to use curl by making a function like this to update session through CI and make a function setcisession() in my controller
function setcises($key,$val)
{
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"http://www.example.com/controller/setcisession/$key/$val");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
}
My function in Controller to set session
function setcisession($key,$value)
{
$this->load->library('session');
$this->session->set_userdata($key,$value);
}
but not luck I also tried
exec("wget --spider www.example.com/home/setcisession/$key/$val");
Edited: Actually, I create CI session when user login to web with key- USER and and store the emailid which is used by the website many place.
And I had written social login code outside codeigniter application (in core php) directory and like to assign the email-id which returned from social login to CI SESSION Key-'USER'
so that the website can identify the logedd in user
I know that I can do this by implementing social login code in CI
But I Want to know how can we set CI session from core php code residing outside codeigniter application directory
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'
I just want to know if I am able to hand over session variables from Laravel to my custom code. What I mean is: I want to handle log-in through Laravel and pass it to my profile section which is not in Laravel. Most of the routes are handled by a .htaccess file. The goal is to just login with Laravel auth and save that to $_SESSION['user'] var and redirect to /profile. Somehow I don't get that. The session name is the same in both, in Laravel's session.php's cookie name and my custom code's constant. Is there any other factor I should consider ?
Okay here's the code:
namespace Services\Session;
class OldSessionAuth
{
protected $auth;
function __construct()
{
$this->auth = \Auth::user();
}
public function setSession()
{
$_SESSION['user'] = $this->auth->toArray();
$_SESSION['auth'] = 'TRUE';
return true;
}
public function destroy()
{
session_destroy();
session_unset();
}
}
So, this is sort of my Session services, which is initialized only if it passes the Auth from the controller, Now I think I don't need to do that. so I skiped it, Basic Stuffs (Auth::Check()) really. So, I'd just do this in my login method.
$old = new Services\Session\OldSessionAuth();
$old->setSession();
return Redirect::to('/');
The home page is controlled by my custom made MVC and I want to grab the session, which in this case I can't. It shows Array(). There is no session manipulation when retrieving the session.
Laravel already has a pretty good session abstraction so I don't think you needed to use session_start(), $_SESSION etc directly. Sharing an session across two applications is a bit tricky. If you are tied to using the cookie approach, then you have to make sure that the session driver in use is the cookie one. You would also need to ensure that the restrictions on the cookie aren't such that your other application isn't being sent them by the user's browser.
By default, PHP will use a file cookie driver. In this case, what you would have to do in your other application is to read the "PHPSESSID" cookie, set the session ID using session_id() to this and only then would you have access to the session data using the $_SESSION variable in the other application.
This is all pretty hacky though. I would recommend that if you need to share sessions that you make use of a database session driver instead. This way, you are able to share arbitrary session data across applications using a standard interface. In this case, you would just read the "laravel_session" cookie instead to be able to look up the session in the database. There would be many hidden pitfalls if you then wanted to also modify this data from the other application as well though.
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.
just a small question regarding the php session handler,
let's say i want to store the session in a database because i have multiple servers that should have access to the session, i would write my own sessionhandler using the interface as described in http://php.net/manual/en/class.sessionhandler.php,
but how can i use it?
if i do
session_set_save_handler(....);
session_start();
$_SESSION['key'] = 'value';
will it save the data using my handler?
The class that you define and set in set_save_handler() should have all the functions required in the lifetime of a session to be defined. These include read(), write(), destroy() among others.
Once that is defined correctly you can still manipulate sessions the normal way you do, but in the background, the functions you define will get executed based on which session event you are performing.
For e.g. $_SESSION['key'] = 'value' will perform the write() function (in which you might have coded a database save routine)
You can read more about it at: http://us3.php.net/manual/en/function.session-set-save-handler.php