File tree Expand file tree Collapse file tree 2 files changed +32
-27
lines changed
Expand file tree Collapse file tree 2 files changed +32
-27
lines changed Original file line number Diff line number Diff line change @@ -817,40 +817,28 @@ public function removeTempdata(string $key)
817817 */
818818 public function markAsTempdata ($ key , int $ ttl = 300 ): bool
819819 {
820- $ ttl += Time::now ()->getTimestamp ();
820+ $ _SESSION ['__ci_vars ' ] ??= [];
821+ $ time = Time::now ()->getTimestamp ();
822+ $ keys = is_array ($ key ) ? $ key : [$ key ];
821823
822- if (is_array ($ key )) {
823- $ temp = [];
824-
825- foreach ($ key as $ k => $ v ) {
826- // Do we have a key => ttl pair, or just a key?
827- if (is_int ($ k )) {
828- $ k = $ v ;
829- $ v = $ ttl ;
830- } elseif (is_string ($ v )) {
831- $ v = Time::now ()->getTimestamp () + $ ttl ;
832- } else {
833- $ v += Time::now ()->getTimestamp ();
834- }
835-
836- if (! array_key_exists ($ k , $ _SESSION )) {
837- return false ;
838- }
824+ if (array_is_list ($ keys )) {
825+ $ keys = array_fill_keys ($ keys , $ ttl );
826+ }
839827
840- $ temp [$ k ] = $ v ;
828+ foreach ($ keys as $ sessionKey => $ timeToLive ) {
829+ if (! array_key_exists ($ sessionKey , $ _SESSION )) {
830+ return false ;
841831 }
842832
843- $ _SESSION ['__ci_vars ' ] = isset ($ _SESSION ['__ci_vars ' ]) ? array_merge ($ _SESSION ['__ci_vars ' ], $ temp ) : $ temp ;
833+ if (is_int ($ timeToLive )) {
834+ $ timeToLive += $ time ;
835+ } else {
836+ $ timeToLive = $ time + $ ttl ;
837+ }
844838
845- return true ;
839+ $ _SESSION [ ' __ci_vars ' ][ $ sessionKey ] = $ timeToLive ;
846840 }
847841
848- if (! isset ($ _SESSION [$ key ])) {
849- return false ;
850- }
851-
852- $ _SESSION ['__ci_vars ' ][$ key ] = $ ttl ;
853-
854842 return true ;
855843 }
856844
Original file line number Diff line number Diff line change @@ -462,6 +462,23 @@ public function testSetTempDataArraySingleTTL(): void
462462 $ this ->assertLessThanOrEqual ($ _SESSION ['__ci_vars ' ]['baz ' ], $ time + 200 );
463463 }
464464
465+ /**
466+ * @see https://github.com/codeigniter4/CodeIgniter4/issues/9534
467+ */
468+ public function testSetTempDataOnArrayData (): void
469+ {
470+ $ session = $ this ->getInstance ();
471+ $ session ->start ();
472+
473+ $ time = time ();
474+
475+ $ session ->setTempdata (['foo1 ' => 'bar1 ' ], null , 200 );
476+ $ session ->setTempdata ('foo2 ' , 'bar2 ' , 200 );
477+
478+ $ this ->assertLessThanOrEqual ($ _SESSION ['__ci_vars ' ]['foo1 ' ], $ time + 200 );
479+ $ this ->assertLessThanOrEqual ($ _SESSION ['__ci_vars ' ]['foo2 ' ], $ time + 200 );
480+ }
481+
465482 public function testGetTestDataReturnsAll (): void
466483 {
467484 $ session = $ this ->getInstance ();
You can’t perform that action at this time.
0 commit comments