Skip to content

Commit 1179907

Browse files
committed
refactor: Session guide
1 parent 972a32b commit 1179907

File tree

13 files changed

+58
-44
lines changed

13 files changed

+58
-44
lines changed

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ Method Signature Changes
7272
Removed Deprecated Items
7373
========================
7474

75-
- **Text Helper:** The deprecated types in ``random_string()`` function: ``basic``, ``md5``, and ``sha1`` has been removed.
7675
- **BaseModel:** The deprecated method ``transformDataRowToArray()`` has been removed.
77-
- **CodeIgniter:** The deprecated ``CodeIgniter\CodeIgniter::resolvePlatformExtensions()`` has been removed.
7876
- **Cache:** The deprecated return type ``false`` for ``CodeIgniter\Cache\CacheInterface::getMetaData()`` has been replaced with ``null`` type.
77+
- **CodeIgniter:** The deprecated ``CodeIgniter\CodeIgniter::resolvePlatformExtensions()`` has been removed.
78+
- **Session:** The deprecated properties ``$sessionDriverName``, ``$sessionCookieName``,
79+
``$sessionExpiration``, ``$sessionSavePath``, ``$sessionMatchIP``,
80+
``$sessionTimeToUpdate``, and ``$sessionRegenerateDestroy`` in ``CodeIgniter\Session`` has been removed.
81+
- **Session:** The deprecated method ``CodeIgniter\Session::stop()`` has been removed.
82+
- **Text Helper:** The deprecated types in ``random_string()`` function: ``basic``, ``md5``, and ``sha1`` has been removed.
7983

8084
************
8185
Enhancements
@@ -141,7 +145,8 @@ Message Changes
141145
***************
142146

143147
- Added ``Email.invalidSMTPAuthMethod``, ``Email.failureSMTPAuthMethod``, ``CLI.signals.noPcntlExtension``, ``CLI.signals.noPosixExtension`` and ``CLI.signals.failedSignal``.
144-
- Deprecated ``Email.failedSMTPLogin`` and ``Image.libPathInvalid``
148+
- Deprecated ``Email.failedSMTPLogin`` and ``Image.libPathInvalid``.
149+
- Changed ``Session.missingDatabaseTable``.
145150

146151
*******
147152
Changes

user_guide_src/source/libraries/sessions.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ The ``$config`` parameter is optional - your application configuration.
2929
If not provided, the services register will instantiate your default
3030
one.
3131

32-
Once loaded, the Sessions library object will be available using::
33-
34-
$session
32+
Once loaded, the Sessions library object will be available using ``$session``.
3533

3634
Alternatively, you can use the helper function that will use the default
3735
configuration options. This version is a little friendlier to read,
@@ -118,7 +116,8 @@ Retrieving Session Data
118116
=======================
119117

120118
Any piece of information from the session array is available through the
121-
``$_SESSION`` superglobal:
119+
``$_SESSION`` superglobal. For example, to assign a previously stored ``name`` item to the ``$name``
120+
variable, you will do this:
122121

123122
.. literalinclude:: sessions/004.php
124123

@@ -134,12 +133,6 @@ Or even through the session helper method:
134133

135134
.. literalinclude:: sessions/007.php
136135

137-
Where ``item`` is the array key corresponding to the item you wish to fetch.
138-
For example, to assign a previously stored ``name`` item to the ``$name``
139-
variable, you will do this:
140-
141-
.. literalinclude:: sessions/008.php
142-
143136
.. note:: The ``get()`` method returns null if the item you are trying
144137
to access does not exist.
145138

@@ -191,7 +184,7 @@ Pushing New Value to Session Data
191184
=================================
192185

193186
The ``push()`` method is used to push a new value onto a session value that is an array.
194-
For instance, if the ``hobbies`` key contains an array of hobbies, you can add a new value onto the array like so:
187+
For instance, if the ``hobbies`` key contains an array of hobbies, you can add a new value or replace the previous value onto the array like so:
195188

196189
.. literalinclude:: sessions/015.php
197190

@@ -402,7 +395,7 @@ All session data (including flashdata and tempdata) will be destroyed permanentl
402395
stop()
403396
------
404397

405-
.. deprecated:: 4.3.5
398+
.. important:: This method was deprecated as of version v4.3.5 and was removed in v4.7.0.
406399

407400
The session class also has the ``stop()`` method.
408401

@@ -453,7 +446,7 @@ Preference Default Opti
453446
**cookieName** ci_session [A-Za-z\_-] characters only The name used for the session cookie.
454447
**expiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last.
455448
If you would like a non-expiring session (until browser is closed) set the value to zero: 0
456-
**savePath** null None Specifies the storage location, depends on the driver being used.
449+
**savePath** WRITEPATH . 'session' None Specifies the storage location, depends on the driver being used.
457450
**matchIP** false true/false (boolean) Whether to validate the user's IP address when reading the session cookie.
458451
Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
459452
will likely set this to false.
@@ -552,6 +545,10 @@ Instead, you should do something like this, depending on your environment:
552545
chmod 0700 /<path to your application directory>/writable/sessions/
553546
chown www-data /<path to your application directory>/writable/sessions/
554547
548+
Since the built-in mechanism does not have automatic cleaning of expired sessions,
549+
you will notice that the *saveDir* directory may overflow with files.
550+
To solve this problem, you will need to configure the **cron** or **Task Scheduler** to delete outdated files.
551+
555552
Bonus Tip
556553
---------
557554

@@ -608,10 +605,10 @@ And then of course, create the database table.
608605
For MySQL::
609606

610607
CREATE TABLE IF NOT EXISTS `ci_sessions` (
611-
`id` varchar(128) NOT null,
612-
`ip_address` varchar(45) NOT null,
613-
`timestamp` timestamp DEFAULT CURRENT_TIMESTAMP NOT null,
614-
`data` blob NOT null,
608+
`id` varchar(128) NOT NULL,
609+
`ip_address` varchar(45) NOT NULL,
610+
`timestamp` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
611+
`data` blob NOT NULL,
615612
KEY `ci_sessions_timestamp` (`timestamp`)
616613
);
617614

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$item = $_SESSION['item'];
3+
$name = $_SESSION['name'];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$item = $session->get('item');
3+
$name = $session->get('name');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$item = $session->item;
3+
$name = $session->name;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$item = session('item');
3+
$name = session('name');

user_guide_src/source/libraries/sessions/008.php

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
<?php
22

3-
$session->push('hobbies', ['sport' => 'tennis']);
3+
/**
4+
* [hobbies] => Array
5+
* (
6+
* [music] => rock
7+
* [sport] => running
8+
* )
9+
*/
10+
$session->set('hobbies', [
11+
'music' => 'rock',
12+
'sport' => 'running',
13+
]);
14+
15+
/**
16+
* [hobbies] => Array
17+
* (
18+
* [food] => cooking
19+
* [music] => rock
20+
* [sport] => tennis
21+
* )
22+
*/
23+
$session->push('hobbies', [
24+
'food' => 'cooking',
25+
'sport' => 'tennis',
26+
]);

user_guide_src/source/libraries/sessions/039.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Config;
44

55
use CodeIgniter\Config\BaseConfig;
6-
use CodeIgniter\Session\Handlers\FileHandler;
6+
use CodeIgniter\Session\Handlers\DatabaseHandler;
77

88
class Session extends BaseConfig
99
{
1010
// ...
11-
public string $driver = 'CodeIgniter\Session\Handlers\DatabaseHandler';
11+
public string $driver = DatabaseHandler::class;
1212

1313
// ...
1414
public string $savePath = 'ci_sessions';

user_guide_src/source/libraries/sessions/040.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Config;
44

55
use CodeIgniter\Config\BaseConfig;
6-
use CodeIgniter\Session\Handlers\FileHandler;
6+
use CodeIgniter\Session\Handlers\DatabaseHandler;
77

88
class Session extends BaseConfig
99
{

0 commit comments

Comments
 (0)