Skip to content

Commit 7afe0e3

Browse files
committed
refactor: Session guide
1 parent 38f8c45 commit 7afe0e3

File tree

15 files changed

+57
-61
lines changed

15 files changed

+57
-61
lines changed

user_guide_src/source/changelogs/v4.3.5.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ SECURITY
1616
See the `Security advisory GHSA-m6m8-6gq8-c9fj <https://github.com/codeigniter4/CodeIgniter4/security/advisories/GHSA-m6m8-6gq8-c9fj>`_
1717
for more information.
1818
- Fixed that ``Session::stop()`` did not destroy the session.
19-
See :ref:`Session Library <session-stop>` for details.
2019

2120
Changes
2221
*******
@@ -30,7 +29,7 @@ Changes
3029
Deprecations
3130
************
3231

33-
- **Session:** The :ref:`Session::stop() <session-stop>` method is deprecated.
32+
- **Session:** The ``Session::stop()`` method is deprecated.
3433
Use the :ref:`Session::destroy() <session-destroy>` instead.
3534

3635
Bugs Fixed

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,18 @@ Removed Deprecated Items
146146
========================
147147

148148
- **BaseModel:** The deprecated method ``transformDataRowToArray()`` has been removed.
149-
- **CodeIgniter:** The deprecated ``CodeIgniter\CodeIgniter::resolvePlatformExtensions()`` has been removed.
150149
- **Cache:** The deprecated return type ``false`` for ``CodeIgniter\Cache\CacheInterface::getMetaData()`` has been replaced with ``null`` type.
150+
- **CodeIgniter:** The deprecated ``CodeIgniter\CodeIgniter::resolvePlatformExtensions()`` has been removed.
151151
- **IncomingRequest:** The deprecated methods has been removed:
152152
- ``CodeIgniter\HTTP\IncomingRequest\detectURI()``
153153
- ``CodeIgniter\HTTP\IncomingRequest\detectPath()``
154154
- ``CodeIgniter\HTTP\IncomingRequest\parseRequestURI()``
155155
- ``CodeIgniter\HTTP\IncomingRequest\parseQueryString()``
156156
- **IncomingRequest:** The deprecated ``$config`` parameter has been removed from ``CodeIgniter\HTTP\IncomingRequest::setPath()``, and the method visibility has been changed from ``public`` to ``private``.
157+
- **Session:** The deprecated properties ``$sessionDriverName``, ``$sessionCookieName``,
158+
``$sessionExpiration``, ``$sessionSavePath``, ``$sessionMatchIP``,
159+
``$sessionTimeToUpdate``, and ``$sessionRegenerateDestroy`` in ``CodeIgniter\Session`` has been removed.
160+
- **Session:** The deprecated method ``CodeIgniter\Session::stop()`` has been removed.
157161
- **Text Helper:** The deprecated types in ``random_string()`` function: ``basic``, ``md5``, and ``sha1`` has been removed.
158162

159163
************
@@ -221,7 +225,8 @@ Message Changes
221225
***************
222226

223227
- Added ``Email.invalidSMTPAuthMethod``, ``Email.failureSMTPAuthMethod``, ``CLI.signals.noPcntlExtension``, ``CLI.signals.noPosixExtension`` and ``CLI.signals.failedSignal``.
224-
- Deprecated ``Email.failedSMTPLogin`` and ``Image.libPathInvalid``
228+
- Deprecated ``Email.failedSMTPLogin`` and ``Image.libPathInvalid``.
229+
- Changed ``Session.missingDatabaseTable``.
225230

226231
*******
227232
Changes

user_guide_src/source/installation/upgrade_435.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ because it is exactly the same as the ``Session::destroy()`` method. So use the
4343

4444
If you have code to depend on the bug, replace it with ``session_regenerate_id(true)``.
4545

46-
See also :ref:`Session Library <session-stop>`.
47-
4846
Project Files
4947
*************
5048

user_guide_src/source/libraries/sessions.rst

Lines changed: 13 additions & 31 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

@@ -397,21 +390,6 @@ All session data (including flashdata and tempdata) will be destroyed permanentl
397390
.. note:: You do not have to call this method from usual code. Cleanup session
398391
data rather than destroying the session.
399392

400-
.. _session-stop:
401-
402-
stop()
403-
------
404-
405-
.. deprecated:: 4.3.5
406-
407-
The session class also has the ``stop()`` method.
408-
409-
.. warning:: Prior to v4.3.5, this method did not destroy the session due to a bug.
410-
411-
Starting with v4.3.5, this method has been modified to destroy the session.
412-
However, it is deprecated because it is exactly the same as the ``destroy()``
413-
method. Use the ``destroy()`` method instead.
414-
415393
Accessing Session Metadata
416394
==========================
417395

@@ -453,7 +431,7 @@ Preference Default Opti
453431
**cookieName** ci_session [A-Za-z\_-] characters only The name used for the session cookie.
454432
**expiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last.
455433
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.
434+
**savePath** WRITEPATH . 'session' None Specifies the storage location, depends on the driver being used.
457435
**matchIP** false true/false (boolean) Whether to validate the user's IP address when reading the session cookie.
458436
Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
459437
will likely set this to false.
@@ -552,6 +530,10 @@ Instead, you should do something like this, depending on your environment:
552530
chmod 0700 /<path to your application directory>/writable/sessions/
553531
chown www-data /<path to your application directory>/writable/sessions/
554532
533+
Since the built-in mechanism does not have automatic cleaning of expired sessions,
534+
you will notice that the *saveDir* directory may overflow with files.
535+
To solve this problem, you will need to configure the **cron** or **Task Scheduler** to delete outdated files.
536+
555537
Bonus Tip
556538
---------
557539

@@ -608,10 +590,10 @@ And then of course, create the database table.
608590
For MySQL::
609591

610592
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,
593+
`id` varchar(128) NOT NULL,
594+
`ip_address` varchar(45) NOT NULL,
595+
`timestamp` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
596+
`data` blob NOT NULL,
615597
KEY `ci_sessions_timestamp` (`timestamp`)
616598
);
617599

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+
]);

0 commit comments

Comments
 (0)