Vlad S
Vlad S

Reputation: 113

$_SESSIONS in multi dimensional arrays

I have the following function to get info from Sessions, so

public function get($_GET)
{
    return $_SESSION['user']['info'][$_GET];
}

and when I try to get some data something weird happen

$this->get('id')
Output: i
Exception: Vlad

and with $_SESSION['user']['info']['id']; it works perfectly

Output: Vlad

Upvotes: 0

Views: 82

Answers (4)

Smit
Smit

Reputation: 1569

Change the name of the argument you are passing to the function. Replace $_GET by any other variable

Upvotes: 0

xdazz
xdazz

Reputation: 160963

$_GET is super global variable, don't use it.

public function get($id) {
  if (isset($_SESSION['user']['info'][$id])){
    return $_SESSION['user']['info'][$id];
  }
  return null;
}

Upvotes: 0

deceze
deceze

Reputation: 522626

$_GET is a reserved name for the super-global $_GET. You'll probably get unexpected results if you try to use it in any other capacity. Change it to a regular $get or something like that.

Upvotes: 3

Fluffeh
Fluffeh

Reputation: 33542

You probably want to use the $_GET as an array like:

$_SESSION['user']['info'][$_GET['someField']];

Upvotes: 0

Related Questions