We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



93.7k
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.