Customer Portal

Get Involved. Join the Conversation.

Topic

    Ronny Oskarsson
    Session variables
    Topic posted January 21, 2009 by Ronny OskarssonGreen Ribbon: 100+ Points, last edited October 29, 2011 
    2232 Views, 8 Comments
    Title:
    Session variables
    Content:

    I am trying to use Session variables to pass information from one page to another but without success

     

    I am creating a session variable

     

    $_SESSION['test'] = 'Some text';

     

    On the same page i do

    echo($_SESSION['test']);

     

    And "Some text" will be printed

     

    But when coming to another page (php page) $_SESSION['test'] does not exist

     

    What am I missing?

     

    Ronny

     

    Comment

     

    • Ernie Turner

      We don't use the PHP native sessions for CP, we use our own which is similar to the session class that Code Igniter uses. You can interact with it by using

       

       

      $this->session->setSessionData($data); $this->session->getSessionData($key);

      Where $data is an associative array of the information you want to store and $this is an instance of the CI superglobal. Where are you trying to set this information at? There are certain places where you will not be able to modify the information within the cookie.

       

       

    • Ronny Oskarsson

      Thank you for a quick reply,

       

      I have a page http://...../app/register/lang/jp (register.php)  where the url have lang/jp as a query string and I want to keep value jp in an session variable to use it as a query string when calling our web server (I have some .NET aspx pages which I have iframed in php pages). If the customer is not logged in I also want the value to be kept after logging in.

      Ronny

    • Ernie Turner
      What version of CP are you running? I know this would probably possible if you are running on the November '08 version by using hooks. I'm not sure if it would be possible on any version before that.
    • Ronny Oskarsson

      Ok,

       

      I am running August '08 version

       

      Ronny

    • Ronny Oskarsson

      When trying setSessionData I get the following error, does it means that it is not possible?

       

      Just a reflection shouldn't setSessionData have two arguments (key and value)?

       

      A PHP Error was encountered

      Severity: Warning

      Message: Invalid argument supplied for foreach()

      Filename: classes/Session.php

      Line Number: 188

      A PHP Error was encountered

      Severity: Warning

      Message: Cannot modify header information - headers already sent by (output started at /cgi-bin/flir.cfg/scripts/euf/application/development/source/controllers/page.php(122) : eval()'d code:2)

      Filename: classes/Session.php

      Line Number: 207

    • Ernie Turner

      No, as I had said above, the setSessionData() function takes an associative array. That way you can set multiple items into the session with a single function call.

       

       

      $this->session->setSessionData(array('key1'=>'val1', 'key2'=>'val2'));

      Where are you trying to call this function from? I know there are some issues in the August '08 version where it might work in development, but unfortunately it will break once you deploy into production.

       

       

    • Ronny Oskarsson

      I didn’t notice that setSessionData takes an array, when doing the call with an array I got rid of one error message.

       

      I am calling from a php file under views/pages/account and calling setSessionData is the only thing the page is doing (it is a test page).

       

      The error I got was
      A PHP Error was encountered
      Severity: Warning
      Message: Cannot modify header information - headers already sent by (output started at /cgi-was(122) : eval()'d code:2)
      Filename: classes/Session.php
      Line Number: 207

    • Ernie Turner
      Ya, its complaining because when you're attempting to modify information in the cookie, the content of the page has already started to be sent to the client, therefore you get an error. All header information (which includes cookie content) must be sent before the page content. As I was saying earlier, I'm not sure there is a place in the August '08 version for you to modify session information before the content is sent to the browser.