Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

getting variable from session array

how do i get a value from inside a session array?

i just want the 'id'

$this->session->set ( 'auth-identity' , [ 'id' => $user->id , 'name' => $user->name , 'profile' => $user->profile->name , ] );

from the Vokuro authorization.

thanks.



69.5k
Accepted
answer
edited Sep '16

If you really need to name your session variable auth-dentity you have to access it like this:

$this->session->get('auth-identity')['id']

I would suggest to name it something like authIdentity or simply auth. This way you can use the "simple" syntax.

$this->session->auth['id']
....

If you accessed $this->session->auth-idenity you will get Fatal error: Unsupported operand type, that's why no dashes name is prefered in my opinion :)

Can't you save only id in session ?

Perhaps this would work: $this->session->{auth-idenity}

If you really need to name your session variable auth-dentity you have to access it like this:

$this->session->get('auth-identity')['id']

I would suggest to name it something like authIdentity or simply auth. This way you can use the "simple" syntax.

$this->session->auth['id']
....

If you accessed $this->session->auth-idenity you will get Fatal error: Unsupported operand type, that's why no dashes name is prefered in my opinion :)

Sadly no.

This $this->session->{auth-idenity} will throw constant notice and will not work.

edited Sep '16

Maybe:

$this->session->{'auth-idenity'}

Yeah, I forgot about quotes. 10x @Jurigag That should do it! Does it @nikolay-mihaylov ?

Dont think quotes will work too. Cant test at the moment, but im almost 100% sure :)

Maybe something like $this->session->$variable or $this->session->{$variable} will work, but not 100% sure with Phalcon session class.