Skip to content

Commit d64171b

Browse files
committed
發布 1.0.0 版本。
0 parents  commit d64171b

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 忽略 VSCode 的工作台設定檔案
2+
*.code-workspace
3+
4+
# 忽略 macOS 系統自動生成的 .DS_Store 檔案與資料夾圖標
5+
.DS_Store
6+
Icon?
7+
8+
# 忽略其他
9+
memo.md

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 邱敬幃 Pardn Chiu
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
21+
SOFTWARE.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# PD\Session
2+
3+
> PD\Session is a flexible PHP session manager with Redis support and filesystem fallback. Provides secure session handling with automated storage selection and built-in security features.
4+
5+
![tag](https://img.shields.io/badge/tag-PHP%20Library-bb4444)
6+
![size](https://img.shields.io/github/size/pardnchiu/PHP-Session/src/Session.php)<br>
7+
![version](https://img.shields.io/packagist/v/pardnchiu/session)
8+
![download](https://img.shields.io/packagist/dm/pardnchiu/session)
9+
10+
## Features
11+
12+
- Dual storage support (Redis/Filesystem)
13+
- Automatic fallback mechanism
14+
- Session security enhancements
15+
- Built-in session lifetime management
16+
- Session ID regeneration
17+
- Creation time tracking
18+
19+
## Security Features
20+
21+
- 7-day session lifetime
22+
- Automatic garbage collection
23+
- Session ID regeneration support
24+
- Secure storage handling
25+
- Fallback mechanism for system resilience
26+
27+
## Dependencies
28+
29+
- `pardnchiu/redis` - For Redis caching support (optional)
30+
- `/storage/sessions` - Write permission on storage directory
31+
32+
## How to Use
33+
34+
### Install
35+
36+
```SHELL
37+
composer require pardnchiu/session
38+
```
39+
40+
```PHP
41+
// Initialize with Redis support
42+
$redis = new PD\Redis();
43+
$session = new PD\Session($redis);
44+
45+
// Basic session operations
46+
$session->set('user_id', 123);
47+
$userId = $session->get('user_id');
48+
$session->delete('user_id');
49+
50+
// Security operations
51+
$session->regenerateId(); // Regenerate session ID
52+
$session->destroy(); // Destroy session
53+
54+
// Session information
55+
$sessionId = $session->getId();
56+
$createdTime = $session->getCreatedTime();
57+
58+
// Initialize without Redis (filesystem only)
59+
$session = new PD\Session();
60+
```
61+
62+
## License
63+
64+
This source code project is licensed under the [MIT](https://github.com/pardnchiu/PHP-Session/blob/main/LICENSE) license.
65+
66+
## Creator
67+
68+
<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;">
69+
70+
<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4>
71+
72+
<a href="mailto:[email protected]" target="_blank">
73+
<img src="https://pardn.io/image/email.svg" width="48" height="48">
74+
</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank">
75+
<img src="https://pardn.io/image/linkedin.svg" width="48" height="48">
76+
</a>
77+
78+
***
79+
80+
©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io)

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "pardnchiu/session",
3+
"description": "PD\\Session is a flexible PHP session manager with Redis support and filesystem fallback, providing secure session handling with automated storage selection.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "邱敬幃 Pardn Chiu",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"php": ">=8.0"
15+
},
16+
"suggest": {
17+
"pardnchiu/redis": "For Redis caching support"
18+
},
19+
"require-dev": {},
20+
"autoload": {
21+
"psr-4": {
22+
"PD\\": "src/"
23+
},
24+
"exclude-from-classmap": [
25+
".gitignore",
26+
"*.md",
27+
"composer.json"
28+
]
29+
},
30+
"autoload-dev": {
31+
"psr-4": {}
32+
},
33+
"scripts": {},
34+
"extra": {
35+
"branch-alias": {}
36+
},
37+
"config": {
38+
"sort-packages": true
39+
}
40+
}

src/Session.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace PD;
4+
5+
class Session
6+
{
7+
// 建構函式,根據 Redis 連線情況設定 Session 儲存方式
8+
public function __construct($REDIS_CLIENT = null)
9+
{
10+
// 顯示所有錯誤,並設定 Session 時間與垃圾回收時間
11+
ini_set("display_errors", 1);
12+
ini_set("display_startup_errors", 1);
13+
ini_set("session.cookie_lifetime", 86400 * 7); // 7天有效
14+
ini_set("session.gc_maxlifetime", 86400 * 7); // 7天過期
15+
error_reporting(E_ALL);
16+
17+
// 如果 Redis 連線可用,則使用 Redis 儲存 Session
18+
if ($REDIS_CLIENT->isConnected()) {
19+
$host = (string) $_ENV["REDIS_HOST"] ?? "localhost";
20+
$port = (int) $_ENV["REDIS_PORT"] ?? 6379;
21+
$password = (string) $_ENV["REDIS_PASSWORD"] ?? '';
22+
$uri = "tcp://" . $host . ":" . $port . "?auth=" . $password . "&database=0&persistent=1"; // Redis 連線 URI
23+
24+
// 設定 Session 儲存方式為 Redis
25+
ini_set("session.save_handler", "redis");
26+
ini_set("session.save_path", $uri);
27+
} else {
28+
// 如果 Redis 不可用,使用本地檔案儲存 Session
29+
$folder = $_SERVER["DOCUMENT_ROOT"] . "/storage/sessions";
30+
31+
// 若資料夾不存在且無法創建,則返回
32+
if (!is_dir($folder) && !mkdir($folder, 0777, true)) {
33+
return;
34+
};
35+
36+
// 如果資料夾不可寫入,則返回
37+
if (!is_writable($folder)) {
38+
return;
39+
};
40+
41+
// 設定 Session 儲存方式為檔案
42+
ini_set("session.save_handler", "files");
43+
ini_set("session.save_path", $folder);
44+
};
45+
46+
// 開始 Session(若未啟動)
47+
if (session_status() == PHP_SESSION_NONE) {
48+
session_start();
49+
};
50+
}
51+
52+
// 設定 Session 值
53+
public function set($key, $value)
54+
{
55+
$_SESSION[$key] = $value;
56+
}
57+
58+
// 取得 Session 值
59+
public function get($key)
60+
{
61+
return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
62+
}
63+
64+
// 刪除 Session 值
65+
public function delete($key)
66+
{
67+
if (isset($_SESSION[$key])) {
68+
unset($_SESSION[$key]);
69+
};
70+
}
71+
72+
// 銷毀整個 Session
73+
public function destroy()
74+
{
75+
session_destroy();
76+
}
77+
78+
// 重新生成 Session ID
79+
public function regenerateId()
80+
{
81+
session_regenerate_id(true);
82+
}
83+
84+
// 取得當前 Session 的 ID
85+
public function getId()
86+
{
87+
return session_id();
88+
}
89+
90+
// 取得 Session 創建時間
91+
public function getCreatedTime()
92+
{
93+
if (!isset($_SESSION["created_time"])) {
94+
$_SESSION["created_time"] = time();
95+
};
96+
97+
return $_SESSION["created_time"];
98+
}
99+
}

0 commit comments

Comments
 (0)