skip to Main Content

In php.ini where can I set the default "secure" parameter value to true for the setcookie() call(): https://www.php.net/manual/en/function.setcookie.php#:~:text=match%20all%20subdomains.-,secure,-Indicates%20that%20the

Tried modifying session.cookie_secure but that seems to only apply to session cookie not regular cookies set by setcookie(…);

2

Answers


  1. To set the default "secure" parameter value to true for the setcookie() function in PHP, you can use the session.cookie_secure directive in your php.ini configuration file. This directive controls whether cookies should only be sent over secure connections. However, this will affect all cookies, including those set by setcookie().

    session.cookie_secure = 1
    

    After updating this setting, you’ll need to restart your web server for the changes to take effect. Note that setting this directive to 1 will make cookies only be sent over secure connections (i.e., HTTPS).

    Login or Signup to reply.
  2. As stated in the manual page, setcookie() already provides a default for secure and it’s false:

     setcookie(
        string $name,
        string $value = "",
        int $expires_or_options = 0,
        string $path = "",
        string $domain = "",
        bool $secure = false, 👈
        bool $httponly = false
    ): bool
    

    Having an INI parameter to override this would probably lead to endless head scratching.

    Not sure what your exact concern is, but:

    • You don’t need to provide all other parameters if those defaults work for you:

      setcookie('foo', 'bar', secure: true);
      
    • You can use the newer signature and define your defaults in a central location:

      define('DEFAULT_COOKIE_OPTIONS', ['secure' => true]);
      setcookie('foo', 'bar', DEFAULT_COOKIE_OPTIONS);
      
    • You can write your own wrapper:

      function customCookie(string $name, string $value): void
      {
          setcookie($name, $value, secure: true);
      }     
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search