PHP code example of stanleykinkelaar / laravel-browser-sessions-lite

1. Go to this page and download the library: Download stanleykinkelaar/laravel-browser-sessions-lite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stanleykinkelaar / laravel-browser-sessions-lite example snippets


return [
    /*
     * Middleware applied to browser sessions routes.
     * Default: ['web', 'auth']
     */
    'middleware' => ['web', 'auth'],

    /*
     * URI prefix for all routes.
     * Default: 'user' (results in /user/browser-sessions)
     */
    'prefix' => 'user',
];

use StanleyKinkelaar\LaravelBrowserSessionsLite\Facades\LaravelBrowserSessionsLite;

$sessions = LaravelBrowserSessionsLite::listForCurrentUser();

foreach ($sessions as $session) {
    echo $session['device_hint'];     // "iOS Device", "Chrome Browser", etc.
    echo $session['ip_address'];       // "192.168.1.1"
    echo $session['is_current'];       // true/false
    echo $session['last_active_at'];   // Carbon instance
}

try {
    $deletedCount = LaravelBrowserSessionsLite::logoutOtherSessionsWithPassword('user-password');

    echo "✅ Logged out {$deletedCount} other sessions";
} catch (\Illuminate\Validation\ValidationException $e) {
    echo "❌ Invalid password";
}

// Useful for admin panels or security automation
$deletedCount = LaravelBrowserSessionsLite::forceLogoutOthersForUser($userId, 'password');

if (LaravelBrowserSessionsLite::hasMultipleSessions()) {
    echo "⚠️ You have active sessions on other devices";
}

$count = LaravelBrowserSessionsLite::getActiveSessionCount();
echo "You have {$count} active sessions";

namespace App\Livewire;

use Livewire\Component;
use StanleyKinkelaar\LaravelBrowserSessionsLite\Facades\LaravelBrowserSessionsLite;

class BrowserSessions extends Component
{
    public $password = '';

    public function logoutOtherSessions()
    {
        $this->validate([
            'password' => 'd', 'Invalid password.');
        }
    }

    public function render()
    {
        return view('livewire.browser-sessions', [
            'sessions' => LaravelBrowserSessionsLite::listForCurrentUser(),
        ]);
    }
}
bash
php artisan session:table
php artisan migrate
bash
php artisan vendor:publish --tag="browser-sessions-lite-config"
bash
php artisan vendor:publish --tag="browser-sessions-lite-views"
json
{
  "sessions": [
    {
      "id": "abc123xyz",
      "ip_address": "192.168.1.1",
      "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...",
      "last_active_at": "2024-01-15 10:30:00",
      "is_current": true,
      "device_hint": "Chrome Browser"
    },
    {
      "id": "def456uvw",
      "ip_address": "192.168.1.50",
      "user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)...",
      "last_active_at": "2024-01-14 18:22:00",
      "is_current": false,
      "device_hint": "iOS Device"
    }
  ],
  "count": 2
}
bash
cd laravel-browser-sessions-lite
composer run prepare
php vendor/bin/testbench serve

src/
├── LaravelBrowserSessionsLiteServiceProvider.php  # Package registration
├── Http/
│   └── Controllers/
│       └── BrowserSessionsController.php          # Handles web + JSON requests
├── Services/
│   └── BrowserSessions.php                        # Business logic (logout, verification)
├── Repositories/
│   └── SessionRepository.php                      # Database queries + device hints
└── Facades/
    └── LaravelBrowserSessionsLite.php             # Facade for easy access