Reputation: 269
I am developing an application in Slim framework where I set sessions where I log the user in. During logging in, i encode the value of user id in the session. Later in functions, I have to retrieve the user id from the sessions to use it in my functions.
Here is the my code:
create session
$app->session->put('current_user', [1, 'hoge_user']);
get session
$current_user = $app->session->get('current_user');
delete session
$app->session->forget('current_user');
Now, while getting a session, how do I echo the value '1' which I have set while creating the session?
Upvotes: 0
Views: 900
Reputation: 11115
The 1
inside the array, would be the index 0 in the array so you would access this through
$current_user = $app->session->get('current_user');
echo $current_user[0]; // 1
echo $current_user[1]; // hoge_user
A small hint you can also define how you access this value, a better approach would be to:
$app->session->put('current_user', ['id' => 1, 'name' => 'hoge_user']);
$current_user = $app->session->get('current_user');
echo $current_user['id'];
Or you may add two sessions variables.
Upvotes: 1