Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace Packback\Lti1p3\ImsStorage;
   4  
   5  use Packback\Lti1p3\Interfaces\ICookie;
   6  
   7  class ImsCookie implements ICookie
   8  {
   9      public function getCookie(string $name): ?string
  10      {
  11          if (isset($_COOKIE[$name])) {
  12              return $_COOKIE[$name];
  13          }
  14          // Look for backup cookie if same site is not supported by the user's browser.
  15          if (isset($_COOKIE['LEGACY_'.$name])) {
  16              return $_COOKIE['LEGACY_'.$name];
  17          }
  18  
  19          return null;
  20      }
  21  
  22      public function setCookie(string $name, string $value, $exp = 3600, $options = []): void
  23      {
  24          $cookie_options = [
  25              'expires' => time() + $exp,
  26          ];
  27  
  28          // SameSite none and secure will be required for tools to work inside iframes
  29          $same_site_options = [
  30              'samesite' => 'None',
  31              'secure' => true,
  32          ];
  33  
  34          setcookie($name, $value, array_merge($cookie_options, $same_site_options, $options));
  35  
  36          // Set a second fallback cookie in the event that "SameSite" is not supported
  37          setcookie('LEGACY_'.$name, $value, array_merge($cookie_options, $options));
  38      }
  39  }