|
1 | | -# FlightPHP Active Record |
2 | | -[](https://packagist.org/packages/flightphp/active-record) |
3 | | -[](https://packagist.org/packages/flightphp/active-record) |
4 | | -[](https://packagist.org/packages/flightphp/active-record) |
5 | | -[](https://packagist.org/packages/flightphp/active-record) |
| 1 | +# FlightPHP Session |
| 2 | +[](https://packagist.org/packages/flightphp/session) |
| 3 | +[](https://packagist.org/packages/flightphp/session) |
| 4 | +[](https://packagist.org/packages/flightphp/session) |
| 5 | +[](https://packagist.org/packages/flightphp/session) |
6 | 6 |
|
7 | | -An active record is mapping a database entity to a PHP object. Spoken plainly, if you have a users table in your database, you can "translate" a row in that table to a `User` class and a `$user` object in your codebase. See [basic example](#basic-example). |
| 7 | +A lightweight, file-based session handler for the Flight framework. It supports non-blocking behavior, optional encryption, and auto-commit functionality. See [basic example](#basic-example). |
8 | 8 |
|
9 | | -## Basic Example |
| 9 | +## Installation |
10 | 10 |
|
11 | | -Let's assume you have the following table: |
| 11 | +Simply install with Composer |
12 | 12 |
|
13 | | -```sql |
14 | | -CREATE TABLE users ( |
15 | | - id INTEGER PRIMARY KEY, |
16 | | - name TEXT, |
17 | | - password TEXT |
18 | | -); |
| 13 | +```bash |
| 14 | +composer require flightphp/session |
19 | 15 | ``` |
20 | 16 |
|
21 | | -Now you can setup a new class to represent this table: |
22 | | - |
23 | | -```php |
24 | | -/** |
25 | | - * An ActiveRecord class is usually singular |
26 | | - * |
27 | | - * It's highly recommended to add the properties of the table as comments here |
28 | | - * |
29 | | - * @property int $id |
30 | | - * @property string $name |
31 | | - * @property string $password |
32 | | - */ |
33 | | -class User extends flight\ActiveRecord { |
34 | | - public function __construct($databaseConnection) |
35 | | - { |
36 | | - parent::__construct($databaseConnection, 'users', [ /* custom values */ ]); |
37 | | - } |
38 | | -} |
39 | | -``` |
| 17 | +## Basic Example |
40 | 18 |
|
41 | | -Now watch the magic happen! |
| 19 | +Let's see how easy it is to use FlightPHP Session: |
42 | 20 |
|
43 | 21 | ```php |
44 | | -// for sqlite |
45 | | -$database_connection = new PDO('sqlite:test.db'); // this is just for example, you'd probably use a real database connection |
| 22 | +// Create a session instance with default settings |
| 23 | +$session = new flight\Session(); |
46 | 24 |
|
47 | | -// for mysql |
48 | | -$database_connection = new PDO('mysql:host=localhost;dbname=test_db&charset=utf8bm4', 'username', 'password'); |
| 25 | +// Store some data |
| 26 | +$session->set('user_id', 123); |
| 27 | +$session->set('username', 'johndoe'); |
| 28 | +$session->set('is_admin', false); |
49 | 29 |
|
50 | | -// or mysqli |
51 | | -$database_connection = new mysqli('localhost', 'username', 'password', 'test_db'); |
52 | | -// or mysqli with non-object based creation |
53 | | -$database_connection = mysqli_connect('localhost', 'username', 'password', 'test_db'); |
| 30 | +// Retrieve data |
| 31 | +echo $session->get('username'); // Outputs: johndoe |
54 | 32 |
|
55 | | -$user = new User($database_connection); |
56 | | -$user->name = 'Bobby Tables'; |
57 | | -$user->password = password_hash('some cool password'); |
58 | | -$user->insert(); |
59 | | -// or $user->save(); |
| 33 | +// Use a default value if the key doesn't exist |
| 34 | +echo $session->get('preferences', 'default_theme'); // Outputs: default_theme |
60 | 35 |
|
61 | | -echo $user->id; // 1 |
| 36 | +// Remove a session value |
| 37 | +$session->delete('is_admin'); |
62 | 38 |
|
63 | | -$user->name = 'Joseph Mamma'; |
64 | | -$user->password = password_hash('some cool password again!!!'); |
65 | | -$user->insert(); |
| 39 | +// Check if a value exists |
| 40 | +if ($session->get('user_id')) { |
| 41 | + echo 'User is logged in!'; |
| 42 | +} |
66 | 43 |
|
67 | | -echo $user->id; // 2 |
| 44 | +// Clear all session data |
| 45 | +$session->clear(); |
68 | 46 | ``` |
69 | 47 |
|
70 | | -And it was just that easy to add a new user! Now that there is a user row in the database, how do you pull it out? |
| 48 | +## Advanced Configuration |
71 | 49 |
|
72 | | -```php |
73 | | -$user->find(1); // find id = 1 in the database and return it. |
74 | | -echo $user->name; // 'Bobby Tables' |
75 | | -``` |
76 | | - |
77 | | -And what if you want to find all the users? |
| 50 | +You can customize the session handler with various configuration options: |
78 | 51 |
|
79 | 52 | ```php |
80 | | -$users = $user->findAll(); |
| 53 | +$session = new flight\Session([ |
| 54 | + 'save_path' => '/custom/path/to/sessions', // Custom directory for storing session files |
| 55 | + 'encryption_key' => 'your-secret-32-byte-key', // Enable encryption with a secure key |
| 56 | + 'auto_commit' => true, // Automatically commit session changes on shutdown |
| 57 | + 'start_session' => true, // Start the session automatically |
| 58 | + 'test_mode' => false, // Enable for testing without affecting PHP's session state |
| 59 | +]); |
81 | 60 | ``` |
82 | 61 |
|
83 | | -What about with a certain condition? |
| 62 | +## Session Security |
| 63 | + |
| 64 | +When dealing with sensitive user data, it's recommended to use encryption: |
84 | 65 |
|
85 | 66 | ```php |
86 | | -$users = $user->like('name', '%mamma%')->findAll(); |
87 | | -``` |
| 67 | +// Create a session with encryption enabled |
| 68 | +$session = new flight\Session([ |
| 69 | + 'encryption_key' => 'a-secure-32-byte-key-for-aes-256-cbc', |
| 70 | +]); |
88 | 71 |
|
89 | | -See how much fun this is? Let's install it and get started! |
| 72 | +// Now all session data will be automatically encrypted when stored |
| 73 | +$session->set('credit_card', '4111-1111-1111-1111'); |
| 74 | +``` |
90 | 75 |
|
91 | | -## Installation |
| 76 | +## Session Regeneration |
92 | 77 |
|
93 | | -Simply install with Composer |
| 78 | +For security purposes, you might want to regenerate the session ID periodically: |
94 | 79 |
|
95 | 80 | ```php |
96 | | -composer require flightphp/active-record |
| 81 | +// Regenerate the session ID and keep the current session data |
| 82 | +$session->regenerate(); |
| 83 | + |
| 84 | +// Regenerate the session ID and delete the old session data |
| 85 | +$session->regenerate(true); |
97 | 86 | ``` |
98 | 87 |
|
99 | 88 | ## Documentation |
100 | 89 |
|
101 | | -Head over to the [documentation page](https://docs.flightphp.com/awesome-plugins/active-record) to learn more about usage and how cool this thing is! :) |
| 90 | +Head over to the [documentation page](https://docs.flightphp.com/awesome-plugins/session) to learn more about usage and how cool this thing is! :) |
102 | 91 |
|
103 | 92 | ## License |
104 | 93 |
|
|
0 commit comments