Welcome Guest, Not a member yet? Register   Sign In
Mixing CI Session and PHP Sessions
#1

[eluser]ryan656[/eluser]
Is there any reason why it would be "bad" to mix sessions? I am storing a bunch of arrays inside session vars and codeigniter doesn't let you access them the same way that native sessions do, for example:

Code:
//Array
$array = array('key' => 'val', 'key2' => 'val2', 'key3' => 'val3');

//Native
$_SESSION['ar'] = $array;
//CI
$this->session->set_userdata('ar', $array);

//Now to access the native values I can do this
$val2 = $_SESSION['ar']['key2'];

//But in CI I have to do this
$ci_array = $this->session->userdata('ar');
$val2 = $ci_array['key2'];

I know it doesn't seem like a huge deal but it annoys the shit out of me that I can't access the values with one line using CI sessions. I am set on using native sessions but the user system and a few other components of the application are already using CI sessions and I don't feel like converting them for this one project.

So again, is there any reason (security, performance, etc.) that this is a bad idea? Or maybe (just thought of this right now) there is third-party session library for CI that allows this?


Thanks,
Ryan
#2

[eluser]WanWizard[/eluser]
Try to solve these issues the CI way, instead of trying to work around it, and make your life complicated.

Extend the session library, by creating a MY_Session.php in application/libraries:
Code:
<?php
class MY_Session extends CI_Session
{
    public function userdata($item)
    {
        if ( ! empty($item) )
        {
            // split the item elements
            $items = explode('.', $item);
            
            // see if we have this item
            $value = parent::userdata( array_shift($items) );

            // process any requested subitems
            foreach( $items as $item )
            {
                // if the retrieved value is an array, and the subitem exists
                if ( is_array($value) && array_key_exists($item, $value) )
                {
                    // make it the new result
                    $value = $value[$item];
                }
                else
                {
                    // requested subitem not found
                    return FALSE;
                }
            }
            // return the session element requested
            return $value;
        }
        else
        {
            // invalid parameter
            return FALSE;
        }
    }
}

This will allow you to request array elements like so
Code:
$result = $this->session->userdata('ar.key2');
#3

[eluser]ryan656[/eluser]
Awesome, thank you! Ideally I would have kept it all CI with something like what you posted, but I'm not that great at OO/CI so I figured it would be easier/quicker to use the sessions I'm familiar with. This concept helps me out in a few different ways though so I really appreciate it.

Since you seem to be pretty smart with PHP and CI, if you don't mind maybe you can help me with my other post here on the forums. I am trying to allow user generated content (WYSIWYG editor from CMS) to be parsed for a template variable like {block=block_id} and execute a PHP function when it's found. It's part of a "static blocks" feature I'm trying to implement for a CMS. Any help would be GREATLY appreciated!

Thanks,
Ryan
#4

[eluser]WanWizard[/eluser]
See my response in that thread.




Theme © iAndrew 2016 - Forum software by © MyBB