0

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

1) Personal Library - Session.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]);
       }
    }

2) Personal Library - Settings.php

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


1) Controller Home.php

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');
        }

} 

2) Controller Auth.php

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 !!!

1
  • i'm using CI on xampp in localhost what you mean ? sorry my english is very bad :/ Commented Oct 1, 2010 at 8:35

6 Answers 6

6

$_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.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry $_SESSION are not cookies i mean :P , i should can retrieve $_SESSION if cookies are turned off don't you think?
i can't use cookie for project establishment
It's very simple: sessions are managed via cookies or via a URL parameter. If you have cookies turned off, you must include the session URL paramter on every page load, otherwise you won't be able to retrieve session data. See the PHP manual page on sessions to confirm this: php.net/manual/en/intro.session.php
yes , now i'm trying to take back $_SESSION data with a normal php scripts out of the framework, but i still can't retrieve $_SESSION when cookies are disabled :/ , i think this is a php.ini problem ,don't you think?
5

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!

Comments

3

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.

3 Comments

oks i'll use a bigger post :) let me 1 min :P i will post all code
This just stores session data, nothing to do with SID Tracking, Its purely down to cookies or GET Parameters, this is just a different driver for storing session data.
so there's no way to use personal $_SESSION array? :O
2

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.

2 Comments

yes i have write session_start at beginning of 1)personal library as you can see, i think CI starts this option when loading library , or does is not true? :P
Sorry your code was badly formatted and I missed it. Why are you creating a library for this? If you want to work with $_SESSION then just do it, and modify as usual. I have worked with $_SESSION several times using this exact method.
0

There's a cookie that stores PHPSESSID browser side.

2 Comments

sorry does it means that i can't retrieve $_SESSION i setted?
need i to try setting other session id? :P
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.