Skip to content

Commit b36eac9

Browse files
committed
Deprecate calling session_set_save_handler() with more than 2 arguments
1 parent 688c6f3 commit b36eac9

35 files changed

+297
-207
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ PHP 8.4 UPGRADE NOTES
173173
. Calling ReflectionMethod::__construct() with 1 argument is deprecated.
174174
Use ReflectionMethod::createFromMethodName() instead.
175175

176+
- Session:
177+
. Calling session_set_save_handler() with more than 2 arguments is
178+
deprecated. Use the 2-parameter signature instead.
179+
176180
========================================
177181
5. Changed Functions
178182
========================================

ext/session/session.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,11 @@ PHP_FUNCTION(session_set_save_handler)
20922092
RETURN_TRUE;
20932093
}
20942094

2095+
zend_error(E_DEPRECATED, "Calling session_set_save_handler() with more than 2 arguments is deprecated");
2096+
if (UNEXPECTED(EG(exception))) {
2097+
RETURN_THROWS();
2098+
}
2099+
20952100
/* Procedural version */
20962101
zend_fcall_info open_fci = {0};
20972102
zend_fcall_info_cache open_fcc;

ext/session/tests/session_module_name_variation2.phpt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ ob_start();
1111

1212
echo "*** Testing session_module_name() : variation ***\n";
1313

14-
function open($save_path, $session_name) { }
15-
function close() { }
16-
function read($id) { }
17-
function write($id, $session_data) { }
18-
function destroy($id) { }
19-
function gc($maxlifetime) { }
14+
class MySessionHandler implements SessionHandlerInterface {
15+
public function open($save_path, $session_name): bool { return false; }
16+
public function close(): bool { return false; }
17+
public function read($id): string|false { return false; }
18+
public function write($id, $session_data): bool { return false; }
19+
public function destroy($id): bool { return false; }
20+
public function gc($maxlifetime): int { return 1; }
21+
}
2022

2123
var_dump(session_module_name("files"));
22-
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
24+
session_set_save_handler(new MySessionHandler());
2325
var_dump(session_module_name());
2426

2527
ob_end_flush();

ext/session/tests/session_module_name_variation3.phpt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ session
1414
ob_start();
1515

1616
echo "*** Testing session_module_name() : variation ***\n";
17-
function open($save_path, $session_name) {
18-
throw new Exception("Stop...!");
19-
}
2017

21-
function close() { return true; }
22-
function read($id) { return ''; }
23-
function write($id, $session_data) { return true; }
24-
function destroy($id) { return true; }
25-
function gc($maxlifetime) { return true; }
18+
class MySessionHandler implements SessionHandlerInterface {
19+
public function open($save_path, $session_name): bool {
20+
throw new Exception("Stop...!");
21+
}
22+
public function close(): bool { return true; }
23+
public function read($id): string|false { return ''; }
24+
public function write($id, $session_data): bool { return true; }
25+
public function destroy($id): bool { return true; }
26+
public function gc($maxlifetime): int { return 1; }
27+
}
2628

2729
var_dump(session_module_name("files"));
28-
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
30+
session_set_save_handler(new MySessionHandler());
2931
var_dump(session_module_name());
3032
var_dump(session_start());
3133
var_dump(session_module_name());
@@ -40,7 +42,7 @@ string(4) "user"
4042

4143
Fatal error: Uncaught Exception: Stop...! in %s:%d
4244
Stack trace:
43-
#0 [internal function]: open('', 'PHPSESSID')
45+
#0 [internal function]: MySessionHandler->open('', 'PHPSESSID')
4446
#1 %s(%d): session_start()
4547
#2 {main}
4648
thrown in %s on line %d

ext/session/tests/user_session_module/basic_set_save_handler_test.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ session.serialize_handler=php
1313
error_reporting(E_ALL);
1414
ob_start();
1515

16-
class handler {
16+
class handler implements SessionHandlerInterface {
1717
public $data = 'baz|O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:1;}arr|a:1:{i:3;O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:1;}}';
1818
function open($save_path, $session_name): bool
1919
{
@@ -43,7 +43,7 @@ class handler {
4343
return true;
4444
}
4545

46-
function gc() { return true; }
46+
function gc($max_lifetime): int { return 1; }
4747
}
4848

4949
$hnd = new handler;
@@ -54,7 +54,7 @@ class foo {
5454
function method() { $this->yes++; }
5555
}
5656

57-
session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
57+
session_set_save_handler(new handler());
5858

5959
session_id("test004");
6060
session_start();
@@ -66,7 +66,7 @@ var_dump($_SESSION["arr"]);
6666

6767
session_write_close();
6868

69-
session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
69+
session_set_save_handler(new handler());
7070
session_start();
7171

7272
var_dump($_SESSION["baz"]);
@@ -77,15 +77,15 @@ session_destroy();
7777
--EXPECT--
7878
OPEN: PHPSESSID
7979
READ: test004
80-
object(foo)#2 (2) {
80+
object(foo)#3 (2) {
8181
["bar"]=>
8282
string(2) "ok"
8383
["yes"]=>
8484
int(2)
8585
}
8686
array(1) {
8787
[3]=>
88-
object(foo)#3 (2) {
88+
object(foo)#4 (2) {
8989
["bar"]=>
9090
string(2) "ok"
9191
["yes"]=>
@@ -95,15 +95,15 @@ array(1) {
9595
WRITE: test004, baz|O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:2;}arr|a:1:{i:3;O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:2;}}
9696
OPEN: PHPSESSID
9797
READ: test004
98-
object(foo)#3 (2) {
98+
object(foo)#4 (2) {
9999
["bar"]=>
100100
string(2) "ok"
101101
["yes"]=>
102102
int(2)
103103
}
104104
array(1) {
105105
[3]=>
106-
object(foo)#2 (2) {
106+
object(foo)#3 (2) {
107107
["bar"]=>
108108
string(2) "ok"
109109
["yes"]=>

ext/session/tests/user_session_module/basic_set_save_handler_test02.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ session.serialize_handler=php
1313
error_reporting(E_ALL);
1414
ob_start();
1515

16-
class handler {
16+
class handler implements SessionHandlerInterface {
1717
public $data = 'baz|O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:1;}arr|a:1:{i:3;O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:1;}}';
1818
function open($save_path, $session_name): bool
1919
{
@@ -44,7 +44,7 @@ class handler {
4444
return true;
4545
}
4646

47-
function gc() { return true; }
47+
function gc($max_lifetime): int { return 1; }
4848
}
4949

5050
$hnd = new handler;
@@ -55,7 +55,7 @@ class foo {
5555
function method() { $this->yes++; }
5656
}
5757

58-
session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
58+
session_set_save_handler($hnd);
5959

6060
session_id("test005");
6161
session_start();
@@ -69,7 +69,7 @@ var_dump($_SESSION["arr"]);
6969

7070
session_write_close();
7171

72-
session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
72+
session_set_save_handler($hnd);
7373
session_start();
7474
$_SESSION["baz"]->method();
7575
$_SESSION["arr"][3]->method();
@@ -82,7 +82,7 @@ var_dump($_SESSION["c"]);
8282

8383
session_write_close();
8484

85-
session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
85+
session_set_save_handler($hnd);
8686
session_start();
8787
var_dump($_SESSION["baz"]);
8888
var_dump($_SESSION["arr"]);

ext/session/tests/user_session_module/bug31454.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ try {
2020

2121
echo "Done\n";
2222
?>
23-
--EXPECT--
23+
--EXPECTF--
24+
Deprecated: Calling session_set_save_handler() with more than 2 arguments is deprecated in %s on line %d
2425
session_set_save_handler(): Argument #1 ($open) must be a valid callback, first array member is not a valid class name or object
2526
Done

ext/session/tests/user_session_module/bug32330.phpt

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,45 @@ session.gc_divisor=1
1313
<?php
1414
error_reporting(E_ALL);
1515

16-
function sOpen($path, $name)
17-
{
18-
echo "open: path = {$path}, name = {$name}\n";
19-
return TRUE;
20-
}
16+
class MySessionHandler implements SessionHandlerInterface {
17+
function open($path, $name): bool
18+
{
19+
echo "open: path = {$path}, name = {$name}\n";
20+
return TRUE;
21+
}
2122

22-
function sClose()
23-
{
24-
echo "close\n";
25-
return TRUE;
26-
}
23+
function close(): bool
24+
{
25+
echo "close\n";
26+
return TRUE;
27+
}
2728

28-
function sRead($id)
29-
{
30-
echo "read: id = {$id}\n";
31-
return '';
32-
}
29+
function read($id): string|false
30+
{
31+
echo "read: id = {$id}\n";
32+
return '';
33+
}
3334

34-
function sWrite($id, $data)
35-
{
36-
echo "write: id = {$id}, data = {$data}\n";
37-
return TRUE;
38-
}
35+
function write($id, $data): bool
36+
{
37+
echo "write: id = {$id}, data = {$data}\n";
38+
return TRUE;
39+
}
3940

40-
function sDestroy($id)
41-
{
42-
echo "destroy: id = {$id}\n";
43-
return TRUE;
44-
}
41+
function destroy($id): bool
42+
{
43+
echo "destroy: id = {$id}\n";
44+
return TRUE;
45+
}
4546

46-
function sGC($maxlifetime)
47-
{
48-
echo "gc: maxlifetime = {$maxlifetime}\n";
49-
return TRUE;
47+
function gc($maxlifetime): int
48+
{
49+
echo "gc: maxlifetime = {$maxlifetime}\n";
50+
return 1;
51+
}
5052
}
5153

52-
session_set_save_handler( 'sOpen', 'sClose', 'sRead', 'sWrite', 'sDestroy', 'sGC' );
54+
session_set_save_handler(new MySessionHandler());
5355

5456
// without output buffering, the debug messages will cause all manner of warnings
5557
ob_start();

ext/session/tests/user_session_module/bug60634.phpt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,33 @@ session
1111

1212
ob_start();
1313

14-
function open($save_path, $session_name) {
15-
return true;
14+
class MySessionHandler implements SessionHandlerInterface {
15+
function open($save_path, $session_name): bool {
16+
return true;
17+
}
18+
19+
function close(): bool {
20+
die("close: goodbye cruel world\n");
21+
}
22+
23+
function read($id): string|false {
24+
return '';
25+
}
26+
27+
function write($id, $session_data): bool {
28+
die("write: goodbye cruel world\n");
29+
}
30+
31+
function destroy($id): bool {
32+
return true;
33+
}
34+
35+
function gc($maxlifetime): int {
36+
return 1;
37+
}
1638
}
1739

18-
function close() {
19-
die("close: goodbye cruel world\n");
20-
}
21-
22-
function read($id) {
23-
return '';
24-
}
25-
26-
function write($id, $session_data) {
27-
die("write: goodbye cruel world\n");
28-
}
29-
30-
function destroy($id) {
31-
return true;
32-
}
33-
34-
function gc($maxlifetime) {
35-
return true;
36-
}
37-
38-
session_set_save_handler('open', 'close', 'read', 'write', 'destroy', 'gc');
40+
session_set_save_handler(new MySessionHandler());
3941
session_start();
4042
session_write_close();
4143
echo "um, hi\n";

0 commit comments

Comments
 (0)