Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Cookies {
- public function check() {
- /* DO YOU REALIZE I'M FORCED TO DO ALL OF THESE TRICKS WITH ARRAYS BECAUSE WE CAN'T
- RETRIEVE THE OPTIONS OF A COOKIE THAT HAS BEEN CREATED,
- AND I'M FORCED TO STORE THOSE OPTIONS IN THE VALUE ITSELF OF THE COOKIE ? */
- /* Since august 2022, there's a new limitation of 400 days for expiration date. */
- $max_expiration = time() + 34560000; // (400 * 24 * 60 * 60) = 34560000
- $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
- /* COOKIE #1 */
- if(!isset($_COOKIE["Sondage01"])) {
- $cookie_options = array('expires' => $max_expiration, 'path' => '/', 'domain' => $domain, 'secure' => false, 'httponly' => false, 'samesite' => 'None');
- $cookie_options_string = json_encode($cookie_options);
- // $cookie_options_array = json_decode($cookie_options_string, true); // I keep that as an exemple to how re-obtain the original array.
- $cookie_value = array(
- 'options' => $cookie_options_string, /* Jesus... */
- 'répondu' => false
- );
- $cookie_value_string = json_encode($cookie_value);
- try {
- setcookie("Sondage01", $cookie_value_string, $cookie_options);
- //$_COOKIE["Sondage01"] = $cookie_value_string;
- } catch(Exception $e) {
- echo "<section>";
- echo "<p>Impossible d'écrire le cookie.</p>";
- echo "</section>";
- }
- } //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... */
- /* COOKIE #2 */
- // ...
- }
- /* A LITTLE TRICK TO AVOID EXPIRATION DATE LIMIT. NO, I DON'T WANT TO USE JAVASCRIPT'S LOCALSTORAGE NOR A DATABASE, THANK YOU. */
- private function extend_expiration_date($cookie_name) {
- $cookie_value = json_decode($_COOKIE[$cookie_name], true); // It is a string containing an array, so we transform it back into a real array.
- $cookie_options = json_decode($cookie_value["options"], true); // Same.
- $expiration_date = $cookie_options['expires'];
- if($expiration_date <= time()) {
- $new_options = array_replace($cookie_options, ['expires' => $max_expiration]);
- $new_value = array_replace($cookie_value, ['options' => $new_options]);
- setcookie($cookie_name, $new_value, $new_options);
- }
- }
- public function init() {
- $this->check();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment