ghalambaz/remember-me
Composer 安装命令:
composer require ghalambaz/remember-me
包简介
RememberMe is a PHP library for persist sessions and remember users without need to login again in period of time!
README 文档
README
Remember Me - Session Persistence (In Just 2 Easy Steps!)
users can stay logged in to your website for how long as you want
remember me help you to have remember me functionality for login and users does not need to login every 20 or 30 minute after inactivity, because it will save your session to database and you can save sessions for long as you want! for example 1 year!!!
Installation
Step 1 : add package with composer
composer require ghalambaz/remember-me
Step 2 : create database tables
-- ---------------------------- -- Table structure for tbl_acl_autologin -- ---------------------------- CREATE TABLE `tbl_acl_autologin` ( `id` int(10) UNSIGNED NOT NULL, `token` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `created` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0), `data` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `used` int(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`, `token`) USING BTREE ) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Table structure for tbl_acl_sessions -- ---------------------------- CREATE TABLE `tbl_acl_sessions` ( `sid` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `expiry` int(10) UNSIGNED NOT NULL, `data` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`sid`) USING BTREE ) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; #Just example for tbl_acl_users CREATE TABLE `tbl_acl_users` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` binary(36) NOT NULL, `username` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `password` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `email` char(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `mobile` bigint(20) NOT NULL, `created` datetime(0) NOT NULL, `touched` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0), `img` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `status` enum('pending','block','delete','active') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE, UNIQUE INDEX `fk_unique_mobile`(`mobile`) USING BTREE, UNIQUE INDEX `fk_unique_email`(`email`) USING BTREE, UNIQUE INDEX `fk_unique_username`(`username`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
you must have a table for your users with column that include usernames
in our example i have table tbl_acl_users that have a column username
Usage
step 1 : initialize Properties Object
//initialize Properties Object $properties = new \RememberMe\Properties(); $properties->setDb("user","password","db_name","host"); //your database access info $properties->setTableUsers("tbl_acl_users"); // table name that you already save your users data $properties->setColUsername("username"); // column that save username,email or any id of your users in tbl_users
you need to access this object globally in your project
step 2 : Changing PHP Session Handler
session_set_save_handler(new \RememberMe\RememberMeSessionHandler($properties)); session_start(); //starting session - check for duplication! $_SESSION['active'] = time();
please put step2 in the first line of your code
Usage Examples
how to login users?
after you logged in user with username and password you need to persist session data . so you should need to run persist function
//strucure of persist function function persist(\RememberMe\Properties $properties,$username) { //after checking that session_regenerate_id(true); $_SESSION[$properties->getSessUname()] = $username; $_SESSION[$properties->getSessAuth()] = true; // persisting login $r = new \RememberMe\RememberMe($properties); $r->start(); }
how to check that user is login now or 1 year later?
//example of check that is user is logged in or not (need to login again) function is_loggedin($properties) { if (isset($_SESSION[$properties->getSessAuth()]) || isset($_SESSION[$properties->getSessPersist()])) { return true; } else { $autologin = new \RememberMe\RememberMe($properties); $autologin->login(); if (!isset($_SESSION[$properties->getSessPersist()])) return false; else return true; } }
how to logout?
//example of logout function structure function logout($properties) { $autologin = new \RememberMe\RememberMe($properties); $autologin->logout(true); }
how to change session life time?
$days = 365; $properties->setLifetimeDays($days);
ghalambaz/remember-me 适用场景与选型建议
ghalambaz/remember-me 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 276 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「persistence」 「login」 「session」 「remember me」 「session handler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ghalambaz/remember-me 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ghalambaz/remember-me 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ghalambaz/remember-me 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This bundle provides tools to build persistence-agnostic storage layer.
WeChat OAuth SDK
Secure session middleware for Slim 3 framework
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
Kinikit - PHP Application development framework MVC component
Debugging a problem and need to login as one of your customers? This allows you to authenticate as any of your customers.
统计信息
- 总下载量: 276
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-22