mysql - New session entries are being created in database even when codeigniter session library is not loaded. Why? -
i having rather weird problem chris kacerguis’ codeigniter rest server.
problems:
1) not loading codeigniter session library, new entries being created in ci_sessions
database table, everytime making http request rest api.
2) brand new entry being created (and old entry not being updated) in db, on every http request, when ip address remaining same.
this config.php file:
$config['sess_driver'] = 'database'; $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 0; $config['sess_save_path'] = 'ci_sessions'; $config['sess_match_ip'] = true; $config['sess_time_to_update'] = 300; $config['sess_regenerate_destroy'] = true; $config['cookie_prefix'] = ''; $config['cookie_domain'] = ''; $config['cookie_path'] = '/'; $config['cookie_secure'] = false; $config['cookie_httponly'] = false;
i tried, individually , in combination, following things:
$config['sess_match_ip'] = false; $config['sess_time_to_update'] = 0; $config['sess_regenerate_destroy'] = false;
and
$config['cookie_domain'] = '.mydomain.com';
but nothing worked.
is normal or kind of bug? doing wrong? else having same problem?
another thing that, not facing issue in vanilla codeigniter. there, working fine , expected.
update
i found while struggling second part of problem.
two session entries being created in database while making first http request - 1 client , 1 rest server. second request, client 'version' of cookie being updated while server 'version' being re-generated.
for 1st part of problem:
as pointed out @jameslalor in comments,
you must either autoloading session
or
you using external library is, in turn, loading session library.
for 2nd part of problem:
the below solution may not best, worked me.
multiple sessions creation problem occurs when:
you have both rest server , client within same codeigniter application directory
and
session library auto-loaded
to client, user consumer. session created user, having ip address of user. cookie set on user’s browser session validated , updated (or newly created) based on validation.
to rest server, client consumer. here session created (if both condition 1 , 2 above fulfilled), time, client, , session has ip address of server on client resides (if condition 1 above fulfilled, ip address of same server on app resides). time cookie not set, consumer not browser. hence, session validation fails , new session created each time page loads.
solution:
rest stateless , every request should contain information required fulfil request. therefore, using sessions (whose sole job maintain user’s state) on rest server considered bad practice. job of client maintain user’s session , pass required information rest server on each , every request.
therefore, assuming not needing session within rest server, solution remove session
autoload[‘libraries’]
list, within autoload.php
file, , load library within client constructor (or when need it).
sorry grammatical errors and/or bad english. not native language.
Comments
Post a Comment