Romarain

Cookies.php

Sep 11th, 2025
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2.  
  3. class Cookies {
  4.        
  5.     public function check() {
  6.         /* DO YOU REALIZE I'M FORCED TO DO ALL OF THESE TRICKS WITH ARRAYS BECAUSE WE CAN'T
  7.            RETRIEVE THE OPTIONS OF A COOKIE THAT HAS BEEN CREATED,
  8.            AND I'M FORCED TO STORE THOSE OPTIONS IN THE VALUE ITSELF OF THE COOKIE ? */
  9.            
  10.         /* Since august 2022, there's a new limitation of 400 days for expiration date. */
  11.         $max_expiration = time() + 34560000; // (400 * 24 * 60 * 60) = 34560000
  12.         $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
  13.        
  14.         /* COOKIE #1 */
  15.         if(!isset($_COOKIE["Sondage01"])) {
  16.             $cookie_options = array('expires' => $max_expiration, 'path' => '/', 'domain' => $domain, 'secure' => false, 'httponly' => false, 'samesite' => 'None');
  17.             $cookie_options_string = json_encode($cookie_options);
  18.             // $cookie_options_array = json_decode($cookie_options_string, true); // I keep that as an exemple to how re-obtain the original array.
  19.             $cookie_value = array(
  20.                 'options' => $cookie_options_string, /* Jesus... */
  21.                 'répondu' => false
  22.             );
  23.             $cookie_value_string = json_encode($cookie_value);
  24.  
  25.             try {
  26.                 setcookie("Sondage01", $cookie_value_string, $cookie_options);
  27.                 //$_COOKIE["Sondage01"] = $cookie_value_string;
  28.                
  29.             } catch(Exception $e) {
  30.                 echo "<section>";
  31.                 echo "<p>Impossible d'écrire le cookie.</p>";
  32.                 echo "</section>";
  33.             }
  34.         } //else {extend_expiration_date("Sondage01");} /* Same for this problem of expiration date limit that prevent us to set a cookie for an unlimited amount of time... */
  35.        
  36.         /* COOKIE #2 */
  37.         // ...
  38.     }
  39.    
  40.    
  41.     /* A LITTLE TRICK TO AVOID EXPIRATION DATE LIMIT. NO, I DON'T WANT TO USE JAVASCRIPT'S LOCALSTORAGE NOR A DATABASE, THANK YOU. */
  42.     private function extend_expiration_date($cookie_name) {
  43.         $cookie_value = json_decode($_COOKIE[$cookie_name], true); // It is a string containing an array, so we transform it back into a real array.
  44.         $cookie_options = json_decode($cookie_value["options"], true); // Same.
  45.         $expiration_date = $cookie_options['expires'];
  46.         if($expiration_date <= time()) {
  47.             $new_options = array_replace($cookie_options, ['expires' => $max_expiration]);
  48.             $new_value = array_replace($cookie_value, ['options' => $new_options]);
  49.             setcookie($cookie_name, $new_value, $new_options);
  50.         }
  51.     }
  52.    
  53.     public function init() {
  54.         $this->check();
  55.     }
  56. }
  57.  
  58. ?>
  59.  
Advertisement
Add Comment
Please, Sign In to add comment