How can one troubleshoot issues with setcookie() not working in mobile browsers?

Issue: Setcookie() may not work in mobile browsers due to various reasons such as browser settings, caching issues, or incorrect cookie parameters. To troubleshoot this, ensure that the cookie is being set correctly with valid parameters, check for any browser-specific limitations, and clear browser cache or try accessing the website in incognito mode.

// Example code to set a cookie with proper parameters
$cookie_name = "user";
$cookie_value = "John Doe";
$expiry = time() + (86400 * 30); // 30 days
$cookie_domain = "/";
$secure = true; // set to true if using HTTPS
$samesite = 'Strict'; // or 'Lax' or 'None'

setcookie($cookie_name, $cookie_value, $expiry, $cookie_domain, $secure, true);