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

How to check session set or not?

if(isset($_SESSION['sessionToken'][''profile])) { } The above code was check the session or not in core php. how to handle in phalcon. if($this->session->has("sessionToken")['profile']) { } I tried in phalcon..Not able to check array of session in phalcon. anybody help me



3.4k
edited Jun '17

If I understand, "sessionToken" is an array. I'd tried something like this :

if($this->session->has("sessionToken") && is_array($this->session->get("sessionToken")) 
        && array_key_exists('profile', $this->session->get("sessionToken"))) {}

Thanks Nikolay for typo correction ;)

edited Jun '17

@Elroliv, Just a typo :)

// These
is_array($this->session->has("sessionToken"))
array_key_exists('profile', $this->session->has("sessionToken"))

// Should be 
is_array($this->session->get("sessionToken"))
array_key_exists('profile', $this->session->get("sessionToken"))

// Or
is_array($this->session->sessionToken)
array_key_exists('profile', $this->session->sessionToken)


3.4k
edited Jun '17

You're right, I'd just typed too quickly ;o))

you can use:

´´´php

$this->session->has("session_name") // returns bool $this->session->get("session_name") // returns session value $this->session->set("session_name","value') // sets session value

´´´