From 6a7b8bd8fdef418f11b2fe8f164a5ea9643eb091 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sat, 26 Mar 2022 16:49:10 +0100 Subject: [PATCH] Disable setting language cookie for curl or clients without user agents --- src/MD_STD.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index 6c8517b..d81ea69 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -428,6 +428,12 @@ final class MD_STD { } else { $lang = self::lang_getfrombrowser($allowed_langs, $default_lang, "", false); + + // If the user is a bot or has no user agent at all or one of curl's, + // setting a cookie usually makes little sense. + // On the other hand, setting the cookie prevents effective caching. + if (empty($_SERVER['HTTP_USER_AGENT']) || substr($_SERVER['HTTP_USER_AGENT'], 0, 5) === 'curl/') return $lang; + if (!setcookie('__Host-lang', $lang, $cookie_options)) { throw new Exception("Failed to set language"); }