A
A
Abc Edc2016-01-04 10:11:54
Laravel
Abc Edc, 2016-01-04 10:11:54

How to clear session in laravel?

This is not about cleaning your session (only this is described in the docks how to do it), but about deleting someone else's session.
That is, how to clean the session by id and not by the current user?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
D', 2016-01-04
@gleber1

In general, I lied, there is an opportunity to do this by standard means (I didn’t wake up while writing the first message).
In Laravel, all session drivers implement SessionHandlerInterface .
You can get the handler with:
Get the current session ID with:
So we need to do the following:
- When a user logs in, get his session ID and store it in the database (for example, add a sesison_id field to the users table).
- When an admin needs to log out a user, you just need to call:

$user = // получаем юзера;
session()->getHandler()->destroy($user->session_id);

The destroy method will delete the user's session and kick him out of the system.
Haven't tested, but should work.
You can do it differently. When you need to kick a user out of the system, you can write a parameter to the session (for example, should_logout = true).
Add a middleware that will check with each request whether this parameter is present in the session, and if so, delete the session.
The second option is more versatile, as it allows you to do some actions before the user is kicked out of the system (for example, save part of the data from the session to the database, if necessary).

A
Alexander N++, 2016-01-04
@sanchezzzhak

Delete the session file if the session is stored in files, if it is in the database, then delete it from the database.

<?php
$pathFile = rtrim(ini_get('session.save_path'),'/').'/' . $sessionId;
if(file_exists($pathFile)){
  echo "File exists!";
  //unlink($pathFile);   
}

The main thing is that the scope of the folders is not limited if Apache + open_basedir will have to edit the config
open_basedir none.

E
Eugene, 2016-01-04
@Nc_Soft

https://laravel.com/docs/5.2/session

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question