TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Forums are in read-only mode while we transition to new software.
using native session array
  • I'm trying to use the native session array for a shopping cart i'm working on. When ever I use $_SESSION
    i get "Undefined variable: _SESSION" error . The documentation is not very good on how to use $_SESSION instead of
    $this->session.
    Do i just instantiate it like you would normally with "$this->session = Session::instance()" ? or am I missing something?
    Thanks
  • On your base controller first define:
    protected $session;

    On the base controller constructor call
    $this->session = Session::instance();

    And then use it like this. On the base controller or in any other controller that extends your base controller.
    $this->session->set('myvariable', '1234 your value' );

    Hope it helps.
    Also be sure to check the docs about session flash variables, they are a great way to show messages to your users after they perform any action.
  • $_SESSION['myvariable'] = '1234 your value'; should work fine as well. Just make sure you initialize your session (by calling Session::instance()) before trying to use $_SESSION.
  • Thanks for the replies.

    The reason i was going to go with the regular $_SESSION variable was that i couldn't find any documentaion on multi-dimensional sessions with the kohana syntax. I ended up finding a forum post that mentioned that kohana sessions supported using dot syntax for multi-dimensional sessions.

    (this is for people who may be looking for examples like i was)

    So you would be able set a price for a shopping cart item like this:

    $this->session = Session::instance();
    $id = 1; // the product id
    $this->session->set("cart.$id.price", 10);

    Then you would get it like this:

    $product_price = $this->session->get("cart.$id.price");

    Does anyone know if this will change in kohana 3?
    Either way I think a little bit better documentation in the user guide would help alot
  • KO3 does not support dot-noted syntax for anything. That was one of the dumbest ideas I ever had. You can accomplish the same thing as your example with:

    $_SESSION['cart'][$id]['price'] = 10;
    
  • from http://docs.kohanaphp.com/libraries/session#get

    "The Session library arranges for PHP's inbuilt sessions array, $_SESSION, to use the session libraries driver. This means that accessing session data can be done in the normal way. Note however this has been seen leading to unpredictable behavior if both $_SESSION and the session library methods are used."



    I had similar problem and I used session->set in such a way:

    $data[0] = 'shark';
    $data[1] = 'salmon';
    $data[2] = 'whale';

    $this->session->set(array('fish' => $data));
    $value = $this->session->get('fish');

    and $value is array.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion