Reputation: 23
i'm trying to use native $_SESSION arrays in Codeigniter beacause i can't use $_COOKIES arrays, so i made my own class but seems that Codeigniter continue to save data on cokkies cause if i disable browser cookies i can't retrieve $_SESSION data :O !! incredible....i can't understand why? is codeigniter stopping or removing all $_SESSION data setted ?
is there someone that is still using only $_SESSION arrays ,aborting default $_COOKIE arrays option? i mean ,i would like to have session data not cookie data but it seems to be impossible :O!!!??!!
i have renamed CI original Session.php library as Cookie.php,and than i made two personal classes into application/libraries and i load them by default into autoloader.php
session_start();
class Session {
function set_data($key,$data)
{
if(!$key)
{ echo 'first param passed is null in session set_data';}
if(!$data)
{ echo 'second param passed is null in session set_data';}
if(isset($key) && isset($data))
{
if(isset($_SESSION[$key]))
{
unset($_SESSION[$key]);
}
return $_SESSION[$key] = $data;
}
function keep_data($key)
{
if(!$key)
{ echo 'first param passed is null in session keep_data';}
if(isset($_SESSION[$key]))
{
return htmlentities($_SESSION[$key]);
}
}
class Settings {
function setsitelanguage()
{
$CI =& get_instance();
if($CI->session->keep_data('lang'))
{
$CI->config->config['language'] = $CI->session->keep_data('lang');
}
else
{
$CI->config->config['language'] = "en";
}
}
than i have 2 controllers
class Home extends Controller {
function Home()
{
parent::Controller();
$this->settings->setsitelanguage();
}
function index()
{
$this->load->view('home/home_view');
}
function session()
{
echo $this->session->keep_data('lang');
}
}
class Auth extends Controller {
function usersetlang()
{
$lang = $this->uri->segment(3);
return $this->session->set_data('lang',$lang);
}
}
as shown in http://mysite/index.php/home/session
, i can retrieve my language site stored with session.php by auth.php
the only problem is that is not enough using global $_SESSION[]
,because if i try to retrieve data disabling my browser cookies $_SESSION[]
data doesn’t appears !!!
Upvotes: 0
Views: 8816
Reputation: 61
We could use the $_SESSION
if we do session_start();
The thing is, $_SESSION
won't work across pages on codeigniter.
Quick fix:
Since the pages in mvc are triggered by the controller, we could do this
public function __construct(){
self::$instance =& $this;
foreach (is_loaded() as $var => $class) {
$this->$var =& load_class($class);
}
$this->load =& load_class('Loader', 'core');
$this->load->initialize();
log_message('debug', "Controller Class Initialized");
session_start();
}
to the CI_Controller
class on system/core/Controller.php
It worked!
Upvotes: 5
Reputation: 25060
If you can't propagate the session id via cookies, you can do so via URL.
See: http://www.php.net/manual/en/session.idpassing.php
Upvotes: 0
Reputation: 30766
Since when does CodeIgniter "block" $_SESSION? If you want to use a session, you have to start it. To do that you need to write session_start() somewhere. why not in your index.php?
CodeIgniter is just PHP, not some evil dark magician.
Upvotes: 2
Reputation: 168823
$_SESSION variables may not be in the cookies themselves, but PHP still needs to be able to track the user's session in order for the $_SESSION variables to be persistent, and it uses cookies for that tracking.
PHP does try to maintain session state if cookies are turned off, by passing the session variable in the URL, but it isn't very reliable.
Bottom line - if you want session data in a web environment, you'll need cookies turned on. Sorry.
You say you can't use cookies, but you didn't really explain why. It's fairly unusual these days for people to want to turn off cookies, as they're used virtually everywhere.
Upvotes: 6
Reputation: 17987
Yes you can do that
create this table:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id)
);
and change this in the config:
$config['sess_use_database'] = FALSE;
to
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
should match the name of your table.
To clarify - by default, CI's session system uses a cookie to store the data. Don't know why, it just does. You can change this for security etc by using the database table instead.
You access session values exactly the same, but aren't restricted by cookie maximum file sizes, or security risks.
You can of course use normal $_SESSIONS and create your own script - remember CI is a framework, you aren't required to use all of it.
Upvotes: 3