简体   繁体   中英

How to get value of user id from a session in slim session manager in PHP?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM