0

I am trying to use a session variable like this:

$string = 'abc';    
$_SESSION[$string];

But I get access if i use it like this:

$_SESSION['abc'];

But I get always an error like this:

Notice: Undefined index: abc

Any ideas to solve my problem? :/

4
  • firstly You have to set some value to this session attribute. Commented Oct 5, 2014 at 9:59
  • Are you trying to create a session variable or are you expecting it to have been created already? Commented Oct 5, 2014 at 9:59
  • I expecting it to have been created already. I get the right value if I use $_SESSION['abc'] but $_SESSION[$string] doesn't work for me. Commented Oct 5, 2014 at 10:01
  • Make sure that $_SESSION[$string] = "Some_Value" Commented Oct 5, 2014 at 10:08

1 Answer 1

1

Simply calling

$string = 'abc';    
$_SESSION[$string];

is not enough. It's only becomes available when you assign some value to it.

$string = 'abc';    
$_SESSION[$string] = 'test';

echo $_SESSION['abc']; //test

Also, make sure that session_start() is called on a page you're accessing it.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah echo $_SESSION['abc'] works fine but not echo $session[$string]
$session is not predefined superglobal variable. if you simply want to make it work, then assign a reference like $session =&$_SESSION above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.