|
1 | | -# InitPHP Session Manager |
2 | | - |
3 | | - |
4 | | -## Requirements |
5 | | - |
6 | | -- PHP 7.2 or later |
7 | | -- [InitPHP ParameterBag Library](https://github.com/InitPHP/ParameterBag) |
8 | | - |
9 | | -## Installation |
10 | | - |
11 | | -``` |
12 | | -composer require initphp/sessions |
13 | | -``` |
14 | | - |
15 | | -## Usage |
16 | | - |
17 | | -```php |
18 | | -require_once "vendor/autoload.php"; |
19 | | -use InitPHP\Sessions\Facede\Session; |
20 | | - |
21 | | -Session::start(); |
22 | | - |
23 | | -Session::set('username', 'admin') |
24 | | - ->set('mail', 'admin@example.com'); |
25 | | -// ... |
26 | | -``` |
27 | | - |
28 | | -## Credits |
29 | | - |
30 | | -- [Muhammet ŞAFAK](https://github.com/muhammetsafak) <<info@muhammetsafak.com.tr>> |
31 | | - |
32 | | -## License |
33 | | - |
34 | | -Copyright © 2022 [MIT License](./LICENSE) |
| 1 | +# InitPHP Session Manager |
| 2 | + |
| 3 | + |
| 4 | +## Requirements |
| 5 | + |
| 6 | +- PHP 7.4 or later |
| 7 | + |
| 8 | +**Note :** Adapters used may have different dependencies. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +``` |
| 13 | +composer require initphp/sessions |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +```php |
| 19 | +require_once "vendor/autoload.php"; |
| 20 | +use InitPHP\Sessions\Session; |
| 21 | + |
| 22 | +Session::createImmutable() |
| 23 | + ->start(); |
| 24 | + |
| 25 | + |
| 26 | +Session::set('username', 'admin') |
| 27 | + ->set('mail', 'admin@example.com'); |
| 28 | +/** |
| 29 | + * OR |
| 30 | + * |
| 31 | + * $_SESSION['username'] = 'admin'; |
| 32 | + * $_SESSION['mail'] = 'admin@example.com'; |
| 33 | + */ |
| 34 | + |
| 35 | +echo Session::get('username', 'Undefined'); |
| 36 | +/** |
| 37 | + * OR |
| 38 | + * |
| 39 | + * echo $_SESSION['username'] ?? 'Undefined'; |
| 40 | + */ |
| 41 | +``` |
| 42 | + |
| 43 | +### Redis Adapter Usage |
| 44 | + |
| 45 | +```php |
| 46 | +require_once "vendor/autoload.php"; |
| 47 | +use InitPHP\Sessions\Session; |
| 48 | +use InitPHP\Sessions\Adapters\RedisAdapter; |
| 49 | + |
| 50 | +$adapter = new RedisAdapter([ |
| 51 | + 'host' => '127.0.0.1', // string |
| 52 | + 'port' => 6379, // int |
| 53 | + 'timeout' => 0, // int |
| 54 | + 'password' => null, // null or string |
| 55 | +], 0, 86400, 'sess'); |
| 56 | + |
| 57 | +Session::createImmutable($adapter) |
| 58 | + ->start(); |
| 59 | +``` |
| 60 | + |
| 61 | +### PDO Adapter Usage |
| 62 | + |
| 63 | +```php |
| 64 | +require_once "vendor/autoload.php"; |
| 65 | +use InitPHP\Sessions\Session; |
| 66 | +use InitPHP\Sessions\Adapters\PDOAdapter; |
| 67 | + |
| 68 | +$pdo = new \PDO('mysql:host=localhost;dbname=test', 'root', ''); |
| 69 | + |
| 70 | +$adapter = new PDOAdapter($pdo, 'app_sessions'); |
| 71 | + |
| 72 | +Session::createImmutable($adapter) |
| 73 | + ->start(); |
| 74 | +``` |
| 75 | + |
| 76 | +Example MySQL Table Create SQL : |
| 77 | + |
| 78 | +```sql |
| 79 | +CREATE TABLE `sessions` ( |
| 80 | + `id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, |
| 81 | + `sess_timestamp` timestamp NULL DEFAULT NULL, |
| 82 | + `sess_ip_address` varchar(48) COLLATE utf8mb4_unicode_ci DEFAULT NULL |
| 83 | + `sess_data` text COLLATE utf8mb4_unicode_ci NOT NULL, |
| 84 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
| 85 | +ALTER TABLE `sessions` ADD PRIMARY KEY (`id`); |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +### Memcache Adapter Usage |
| 90 | + |
| 91 | +```php |
| 92 | +require_once "vendor/autoload.php"; |
| 93 | +use InitPHP\Sessions\Session; |
| 94 | +use InitPHP\Sessions\Adapters\MemCacheAdapter; |
| 95 | + |
| 96 | +$adapter = new MemCacheAdapter([ |
| 97 | + 'host' => '127.0.0.1', // string |
| 98 | + 'port' => 11211, // int |
| 99 | + 'weight' => 1, // int |
| 100 | + 'raw' => false, // boolean |
| 101 | + 'prefix' => null, // null or string |
| 102 | + 'ttl' => 86400, // int |
| 103 | +]); |
| 104 | + |
| 105 | +Session::createImmutable($adapter) |
| 106 | + ->start(); |
| 107 | +``` |
| 108 | + |
| 109 | +### Cookie Adapter Usage |
| 110 | + |
| 111 | +```php |
| 112 | +require_once "vendor/autoload.php"; |
| 113 | +use InitPHP\Sessions\Session; |
| 114 | +use InitPHP\Sessions\Adapters\CookieAdapter; |
| 115 | + |
| 116 | +$adapter = new CookieAdapter('sessDataCookieName', 'topSecretAppKey', 86400); |
| 117 | + |
| 118 | +Session::createImmutable($adapter) |
| 119 | + ->start(); |
| 120 | +``` |
| 121 | + |
| 122 | +### MongoDB Adapter Usage |
| 123 | + |
| 124 | +```php |
| 125 | +require_once "vendor/autoload.php"; |
| 126 | +use InitPHP\Sessions\Session; |
| 127 | +use InitPHP\Sessions\Adapters\MongoDBAdapter; |
| 128 | + |
| 129 | +$adapter = new MongoDBAdapter('mongodb://127.0.0.1:27017', 'sessDbName.sessCollectionName'); |
| 130 | + |
| 131 | +Session::createImmutable($adapter) |
| 132 | + ->start(); |
| 133 | +``` |
| 134 | + |
| 135 | +## Credits |
| 136 | + |
| 137 | +- [Muhammet ŞAFAK](https://github.com/muhammetsafak) <<info@muhammetsafak.com.tr>> |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +Copyright © 2022 [MIT License](./LICENSE) |
0 commit comments