44
55namespace Tamedevelopers \Support ;
66
7+ use Tamedevelopers \Support \Str ;
78use Tamedevelopers \Support \Time ;
89
910class Cookie{
@@ -45,7 +46,7 @@ class Cookie{
4546 */
4647 static protected function init ()
4748 {
48- self ::$ name = strtolower ( str_replace ([' ' ], '' , env ('APP_NAME ' , '' )));
49+ self ::$ name = Str:: lower (Str:: replace ([' ' ], '' , env ('APP_NAME ' , '' )));
4950 self ::$ timeName = "__time_ " . self ::$ name ;
5051 self ::$ expireName = "__expire_ " . self ::$ name ;
5152 self ::$ timeFormat = Time::timestamp ('next year ' , 'Y-m-d ' );
@@ -91,16 +92,34 @@ static protected function init()
9192 public static function set ($ name , $ value = null , $ minutes = 0 , $ path = null , $ domain = null , $ secure = null , $ httponly = null , $ force = null )
9293 {
9394 // minutes
94- $ minutes = self ::minutesToExpire ($ minutes );
95+ $ expires = self ::minutesToExpire ($ minutes );
9596
9697 // create default values
9798 [$ path , $ value , $ domain , $ secure , $ httponly , $ force ] = self ::getDefaultPathAndDomain (
9899 $ path , $ value , $ domain , $ secure , $ httponly , $ force
99100 );
100101
101- // set cookie
102- if ( !headers_sent () || $ force === true ) {
103- @setcookie ($ name , $ value , $ minutes , $ path , $ domain , $ secure , $ httponly );
102+ // Prefer new setcookie signature with array options (PHP 7.3+), fallback otherwise
103+ if (!headers_sent () || $ force === true ) {
104+ $ options = [
105+ 'expires ' => $ expires ,
106+ 'path ' => $ path ,
107+ 'domain ' => $ domain ?: '' ,
108+ 'secure ' => (bool ) $ secure ,
109+ 'httponly ' => (bool ) $ httponly ,
110+ 'samesite ' => 'Lax ' , // sensible default
111+ ];
112+
113+ // Try modern signature; if it fails (older PHP), fallback to legacy
114+ try {
115+ if (PHP_VERSION_ID >= 70300 ) {
116+ @setcookie ($ name , (string ) $ value , $ options );
117+ } else {
118+ @setcookie ($ name , (string ) $ value , (int ) $ expires , (string ) $ path , (string ) $ domain , (bool ) $ secure , (bool ) $ httponly );
119+ }
120+ } catch (\Throwable $ e ) {
121+ @setcookie ($ name , (string ) $ value , (int ) $ expires , (string ) $ path , (string ) $ domain , (bool ) $ secure , (bool ) $ httponly );
122+ }
104123 }
105124 }
106125
@@ -143,8 +162,7 @@ public static function expire($name, $path = null, $domain = null)
143162 */
144163 public static function setTime ()
145164 {
146- self ::init ()
147- ->set (self ::$ timeName , self ::$ timeFormat );
165+ self ::init ()->set (self ::$ timeName , self ::$ timeFormat );
148166 }
149167
150168 /**
@@ -154,8 +172,7 @@ public static function setTime()
154172 */
155173 public static function setExpire ()
156174 {
157- self ::init ()
158- ->set (self ::$ expireName , self ::$ expireFormat );
175+ self ::init ()->set (self ::$ expireName , self ::$ expireFormat );
159176 }
160177
161178 /**
@@ -200,9 +217,7 @@ public static function has($name = null)
200217 */
201218 public static function get ($ name = null )
202219 {
203- return self ::has ($ name )
204- ? $ _COOKIE [(string ) $ name ]
205- : null ;
220+ return self ::has ($ name ) ? $ _COOKIE [(string ) $ name ] : null ;
206221 }
207222
208223 /**
@@ -226,24 +241,23 @@ public static function all($name = null)
226241 */
227242 private static function minutesToExpire ($ minutes = 0 )
228243 {
229- // options
230- if (empty ($ minutes )){
231- $ minutes = 0 ;
232- } elseif (is_numeric ($ minutes )){
233- $ minutes = time () + (((int ) $ minutes ) * 60 );
234- } else {
235- $ minutes = strtotime ($ minutes );
236- if ($ minutes === false || $ minutes === -1 ) {
237- // Invalid timestamp format, default to 0 (end of session)
238- return 0 ;
239- }
244+ if (empty ($ minutes )) {
245+ return 0 ;
240246 }
241-
242- return (int ) $ minutes ;
247+ if (is_numeric ($ minutes )) {
248+ return time () + (((int ) $ minutes ) * 60 );
249+ }
250+
251+ $ ts = strtotime ((string ) $ minutes );
252+ if ($ ts === false || $ ts === -1 ) {
253+ // Invalid timestamp format, default to 0 (end of session)
254+ return 0 ;
255+ }
256+ return (int ) $ ts ;
243257 }
244258
245259 /**
246- * Get the path and domain, or the default values .
260+ * Get default path/domain and flags .
247261 *
248262 * @param string|null $path
249263 * @param string|null $value
0 commit comments