Delete session in laravel - laravel

I am using a session_variable as the user_id to get information about the user by querying with the help of the variable. Whenever my user logs in with correct credentials, I create a session variable using:
\Session::put('user_id', $user_id);
And now I have a condition when the user logs out from the website,so how do I remove the session variable from the session using laravel. I tried something like : session()->forget('some_data'); which didn't seem to work for me so what should I do to remove all the session variable I have in the webpage?

You can try with this
Session::flush(); // removes all session data
Session::forget('user_id') // Removes a specific variable

Related

How to get the session data into Codeigniter controller after user logged in?

I want to access session data after the user has logged in. I'm able to access session data in view pages but I want to access in the controller as well to create some database tables with session data like user id, name etc.
You can get session data:
$session = $this->session->userdata('session_name'); //Replace 'session_name' to your session name
print_r($session); //For check
You can details from Click Here
first set session variable in login controller as-
$this->session->set_userdata('userid', $row->iduser);
and use that session variable as-
$this->session->userdata('userid');
Following line will show all session data.
$session_data = $this->session->userdata();
echo "<pre>"; print_r($session_data); exit;

laravel sociallite in boilerplate

I have used laravel boilerplate for login.
I have worked using virtual host and put information on basis of virtualhost.
I have 2 tables:
1. users
2. social_logins
Actually i am authenticating from users table. I need to store information in users and then link it with social_logins user_id i.e, store information in table 2.
The main problem here is it doesn't store data in table. This line is not working:
return $this->getAuthorizationFirst($provider);
I have put redirect so it will redirect to given route but doesn't store data in database.
if I catch callback in controller I can catch the information.
$user = Socialite::driver('facebook')->user();
I have done normally using Socialite plugin in normal laravel, but i don't have any idea in Boilerplate.
Actual lifecycle of this loginThirdParty trait is:
1 The first time this is hit, request is empty
2 It's redirected to the provider and then back here, where request is populated
3 So it then continues creating the user
So If the provider is not an acceptable, this trait kick back. Hopefully you need to recheck your .env for FACEBOOK_CLIENT_ID, FACEBOOK_CLIENT_SECRET and CALLBACK_URL.

Codeigniter remove specific user's session

In Codeigniter, how do I remove specific user's session?
I have set login session like below. And I want to remove specific MemId's LoggedIn(in other words, session for just one user) assuming there are about 1000 users.
$this->session->set_userdata([
'LoggedIn'=>true,'MemId'=>PrimaryKey
]);
When user logged in you are setting two session variables.One is LoggedIn and another 'MemId' which is the primary key of member.So,first retrieve the logged in user member id from database then unset it.Might work properly.
MemId is the member id of user for which you want to remove session.
$this->session->unset_userdata('MemId');

How to keep session id after login in Laravel 5.1

I been using my session id to identify all the items that are added to a shopping cart temporary items table.
What I didn't know is that once the user login into the system the session id changes so I can't query the temporary items table anymore as there is no products containing the new session id value.
I am using file session driver and I was thinking to use db sessions but I think it will be exactly the same.
Any ideas on how to keep the session id after login?
Instead of using user's session_id you can store the cart items in a separate session key.
Like so -
Session::put('cart.items.0', $item_id);
Now you can access it as -
$cart = Session::get('cart');
Not exactly what you asked for, but to me this seems like a better way.

How can I validate current user session id with database stored session ids?

I have found that Code Igniter has already ci_sessions table which stores users session ids in it. I understand the main purpose of that table. Just I want to clarify how I can validate with ci_session table data with my current user login session id?
Basically , I am trying to do User login one place at a time. Multiple login's to be prevented. How can I do this?
You couldn't do that with session ID's since logging in from another location would create a new session ID at that new location.
Store the userID to the session then check it on login.
$this->db->where('userId',$userId);
$result = $this->db->get('ci_session');
if($result->num_rows() > 0)
{
//log out old user, throw error, whatever
} else {
//continue with login
}
By storing the user id in the session data and adding it as a column to the sessions table, you can remove all sessions of that user id on login before the session data is created. This will invalidate any session of the same user. For further explanation on how to do this, see here: https://stackoverflow.com/a/9564433/89211

Resources