We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

EE2: Can I check if session exists? (Plugin)

Development and Programming

esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

This is a somewhat codeigniter/ee2.0 question. Hope it’s the right forum 😊

I want to do this in my plugin:

if($this->EE->session->userdata['language_code'])
{
    $this->return_data = "Language is: " . $this->EE->session->userdata['language_code'];
} else {
    $this->EE->session->userdata['language_code'] = "en";
}

But I keep getting this error-message:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: language_code

Filename: xxx/pi.xxx.php

Line Number: 44

If i try wrapping it in “isset()” it doesn’t throw the error. But after the session variable language_code has been set upon reload the variable isn’t there anymore. It’s like it’s being flashed and the variable “language_code” is yet again empty.

Please help

Thank you

Moved to Plugin by Moderator, title changed

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

No one tried to manipulate sessions in EE2.0 yet?

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

I just looked over the /expressionengine/libraries/Session.php file and I don’t see the “set_userdata” function existing. Is this done on purpose, aren’t you suppose to be able to add to the session->userdata array? Should this be handled in some other way?

Thanks

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

Hi

I’m trying to add something to the userdata session variable but it doesn’t seem to stick. I’ve just put in a simple code to try it:

function code()
    {
        $code = "Code before is: " . $this->EE->session->userdata['code'];
        $this->EE->session->userdata['code'] = "en";
        $code .= "Code after is: " . $this->EE->session->userdata['code'];
        return $code;
}

The first line tells me there’s an undefined index in the userdata array. The second line gives me back “en” as it should.

When I then reload the site the first line should give me “en” back as well since it’s been added to the session, but it doesn’t. It’s empty.

– I already have a thread about this (http://ellislab.com/forums/viewthread/138599/) but haven’t recieved an answer in over 2 days so I thought maybe it was higher activity in this forum for EE2.0 related questions. Sorry if I broke some rule with my double post, if so feel free to remove one thread. Thank you.

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

I’ve tried adding with both array_push, userdata[] and userdata[‘key’], but nothing sticks. The key gets added during that call (pageload) but after next pageload it’s cleared.

Does the userdata somehow “refresh” upon each pageload?

How should I treat my own session variables?

       
Herb's avatar
Herb
224 posts
16 years ago
Herb's avatar Herb

I don’t believe that EE stores the session->userdata in session variables. I think it regenerates the array on each page load after verifying the user’s session.

What you probably want is Session Cache

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

Rock on!! Thanks man I’ll look into this.

       
Ingmar's avatar
Ingmar
29,245 posts
16 years ago
Ingmar's avatar Ingmar

Let us know how you get along 😊

       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

I also had a similar thread but didn’t really get an official answer. That was initially regarding flashdata - which as far as I can tell now, works ONLY on the control panel side, which isn’t clear at all from the user guide. Too bad, as flashdata would be really handy on the front-end.

As far as the session cache goes, that won’t do it - per the manual:

$this->EE->session->cache is an array provided for you to use for “flash” content, i.e. values that you would like to persist during a page load …

Emphasis mine. It only works during page loads, not between page loads (I can confirm this).

I haven’t had a chance to try this yet, but CodeIgniter 1.7.2 uses set_userdata:

$data = array(
                   'username'  => 'johndoe',
                   'email'     => '[email protected]',
                   'logged_in' => TRUE
               );
$this->EE->session->set_userdata($data);

Give that a try.

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

Yes I can also confirm that the session->cache doesn’t work for more then one pageload.

“set_userdata” doesn’t work either. When using it EE gives you:

Fatal error: Call to undefined method EE_Session::set_userdata() in /../system/expressionengine/libraries/Functions.php(614) : eval()'d code on line 18

When looking over the functions.php file the “set_userdata” functions seems to have been removed.

So right now you can’t develop any third-party plugins, extensions or modules using sessions for the frontend?

Could we get some official response to how to do this? Maybe we have missed something in the docs.

Thanks

       
Adam Dorsey's avatar
Adam Dorsey
1,439 posts
16 years ago
Adam Dorsey's avatar Adam Dorsey

esset- I am going to go ahead and merge this thread, with your other open thread, as it is the same issue. Thanks 😊

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

Thanks. But can you move it back to the Technical forums since it’s not really plugin related more a general issue on how to use sessions with EE, even if it’s used in a plugin, module or extension.

Thank you

       
Herb's avatar
Herb
224 posts
16 years ago
Herb's avatar Herb

Okay… I haven’t even got pb2 installed but I’m gonna take a stab at it anyway.

Inside of a plug-in:

class Some_plugin {

  var return_date = '';

  function Some_plugin() {
    $this->EE =& get_instance(); 
  }

  // set the flash data to have on next page load {exp:some_plugin:set_flash lang="en"}
  function set_flash($var_name, $var_value) {
    $var_name = 'lang';
    $var_val = $this->EE->TMPL->fetch_param('lang');
    $this->EE->session->set_flashdata($var_name, $var_value);
    return;
  }

  // get the flash data for session on this page load
  function get_flash($var_name) {
    $this->return_data = $this-EE->session->flashdata($var_name);
    return this->return_data;
  }

  // keep the flash data for the next page load {exp:some_plugin:keep_flash var='lang'}
  function keep_flash($var_name) {
    $var_name = $this->EE->TMPL->fetch_param('var');
    $this->EE->session->keep_flashdata($var_name);
    return;
  }
}

Reference:Public Beta Plug-in docs, Public Beta Session Class, Code Ignitor Session Docs

I’m not sure weather you can make a reference to the EE super object within the template parser, so I’m not sure you can do this as php in a template; however, plug-ins allow you to step out of the template, do your thing, then step back in.

Hope this is useful and… well, right. Since, this is straight of the top of my head.

       
esset's avatar
esset
191 posts
16 years ago
esset's avatar esset

Well it’s right if you want to utilize the flashdata and keep having to set/get it all the time.

Our question right now is more HOW do you utilize the session class? This is why I asked for this thread to be put back into “Technical Support” since it’s more of a general question regarding sessions then how to use it within a plugin since the practice pretty much will be the same for plugins, modules and extensions.

The CI documentation regarding the session class can’t be used since for example “set_userdata” seems to have been removed completly.

// On a different note I tried running session_start() and then use the regular $_SESSION[] variable which works but seems like a big dirty workaround.

       
Herb's avatar
Herb
224 posts
16 years ago
Herb's avatar Herb

I can see where you are going with this, and its value. How about an extension using the session_end to check to see if your variables are set, set them if needed, then append them to the user_data array?

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.