michalsn/codeigniter-session-extended 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

michalsn/codeigniter-session-extended

最新稳定版本:v1.0.0

Composer 安装命令:

composer require michalsn/codeigniter-session-extended

包简介

Managing user sessions stored in the database for the CodeIgniter 4 framework

README 文档

README

This library gives users the ability to view their own active sessions and remove them from devices they no longer use.

It works only with database session handler.

PHPUnit PHPStan Deptrac

PHP CodeIgniter

Requirements

This library requires the application to comply with CodeIgniter 4 authentication recommendations.

Installation

Composer

composer require michalsn/codeigniter-session-extended

Manually

In the example below we will assume, that files from this project will be located in app/ThirdParty/session-extended directory.

Download this project and then enable it by editing the app/Config/Autoload.php file and adding the Michalsn\CodeIgniterSessionExtended namespace to the $psr4 array, like in the below example:

<?php

namespace Config;

use CodeIgniter\Config\AutoloadConfig;

class Autoload extends AutoloadConfig
{
    // ...
    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',
        'Michalsn\CodeIgniterSessionExtended' => APPPATH . 'ThirdParty/session-extended/src',
    ];

    // ...

Now, follow the instructions of configuring session database handler - with one distinction. You have to use Michalsn\SessionExtended\DatabaseHandler class instead of the core one.

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;
use Michalsn\SessionExtended\DatabaseHandler;

class Session extends BaseConfig
{
    // ...
    public string $driver = DatabaseHandler::class;

    // ...
    public string $savePath = 'ci_sessions';

    // ...
}

The last step will be to run a command that will add extra fields to the session table. To do so, run command:

php spark se:install

That's it. You're ready to go.

Example

// app/Controllers/Home.php
<?php

namespace App\Controllers;

use CodeIgniter\Exceptions\PageNotFoundException;
use Michalsn\CodeIgniterSessionExtended\SessionManager;

class Sessions extends BaseController
{
    public function index()
    {
        $sm = new SessionManager();

        $data['sessions'] = $sm->list(user_id());

        return view('sessions/index', $data);
    }

    public function delete()
    {
        if (! $this->request->is('post')) {
            throw new PageNotFoundException();
        }

        $rules = [
            'id' => ['required', 'string', 'max_length[128]'],
        ];

        if (! $this->validate($rules)) {
            return redirect()->back();
        }

        $sm = new SessionManager();

        if ($sm->delete($this->request->getPost('id'), user_id())) {
            return redirect()->back()->with('success', 'Session was successfully deleted.');
        }

        return redirect()->back()->with('error', 'Something went wrong.');
    }
}

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-23