What potential pitfalls should be avoided when using the setcookie function in PHP?

Potential pitfalls when using the setcookie function in PHP include not setting an expiration time, not specifying the path and domain parameters correctly, and not using secure and httpOnly flags for sensitive cookies. To avoid these pitfalls, always set an expiration time, specify the correct path and domain parameters, and use the secure and httpOnly flags when necessary.

// Example of setting a cookie with proper parameters
$cookie_name = "user";
$cookie_value = "John Doe";
$expiration_time = time() + 3600; // expires in 1 hour
$path = "/";
$domain = "example.com";
$secure = true;
$httpOnly = true;

setcookie($cookie_name, $cookie_value, $expiration_time, $path, $domain, $secure, $httpOnly);