D
D
derasoft2021-12-18 14:36:40
PHP
derasoft, 2021-12-18 14:36:40

How to "skip" function arguments?

I want to make the set cookie visible to all site domains, and I know for sure that the function definitely works, but I don't know how to rewrite it so as not to pass the $expires argument.
setcookie("user", $user["login"], 0 , "/");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Spartak (Web-StyleStudio), 2021-12-18
@derasoft

setcookie
PHP >= 8
Named Arguments

setcookie(
  name: 'user',
  value: $user["login"],
  path: '/'
);

PHP >= 7.3.0
Alternative signature
setcookie('user', $user["login"], ['path' => '/']);

You can use the wrapper function, in which you can set your order of arguments and their default values:
function cust_setcookie(
  $name,
  $value = "",
  $path = "/", 
  $expires = 0,
  $domain = "",
  $secure = false,
  $httponly = false
) {
  setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);
}

cust_setcookie('user', $user["login"]);

Or in php.ini set the required default values ​​for path and other arguments, then just use:
setcookie('user', $user["login"]);

D
Dmitry Gordinskiy, 2021-12-18
@DmitriyGordinskiy

Named Arguments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question