Skip to content

Commit 7eca70c

Browse files
committed
v2.0
1 parent 4d568ac commit 7eca70c

20 files changed

+1312
-665
lines changed

.gitignore

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/.idea/
2-
/.vscode/
3-
/.vs/
4-
/vendor/
5-
/composer.lock
1+
/.idea/
2+
/.vscode/
3+
/.vs/
4+
/vendor/
5+
/composer.lock

LICENSE

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2022 InitPHP
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1+
MIT License
2+
3+
Copyright (c) 2022 InitPHP
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.

README.md

Lines changed: 141 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,141 @@
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 &copy; 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 &copy; 2022 [MIT License](./LICENSE)

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "initphp/sessions",
3-
"description": "InitPHP Session Manager",
4-
"keywords": ["php", "session", "initphp"],
3+
"description": "InitPHP Sessions Manager",
4+
"keywords": ["initphp", "sessions", "session", "session handler", "cookie", "memcache", "memcached", "mongodb", "pdo", "mysql", "postgresql", "postgres", "sql", "sqlite", "redis"],
55
"type": "library",
66
"license": "MIT",
77
"autoload": {
@@ -13,13 +13,12 @@
1313
{
1414
"name": "Muhammet ŞAFAK",
1515
"email": "info@muhammetsafak.com.tr",
16-
"role": "Developer",
17-
"homepage": "https://www.muhammetsafak.com.tr"
16+
"homepage": "https://www.muhammetsafak.com.tr",
17+
"role": "Developer"
1818
}
1919
],
2020
"minimum-stability": "stable",
2121
"require": {
22-
"php": ">=7.2",
23-
"initphp/parameterbag": "^1.1"
22+
"php": ">=7.4"
2423
}
2524
}

src/AbstractAdapter.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* AbstractAdapter.php
4+
*
5+
* This file is part of InitPHP Sessions.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2022 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 2.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
namespace InitPHP\Sessions;
15+
16+
abstract class AbstractAdapter implements Interfaces\AdapterInterface
17+
{
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function close()
23+
{
24+
return true;
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
abstract public function destroy($id);
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function gc($max_lifetime)
36+
{
37+
return true;
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function open($path, $name)
44+
{
45+
return true;
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
abstract public function read($id);
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
abstract public function write($id, $data);
57+
58+
59+
}

0 commit comments

Comments
 (0)