Problem with $lifetime in setCookie() function

I'm creating an external login form in my website and I have a problem with the parameter $lifetime in setCookie() function (from XenForo_Helper_Cookie).

This is the setCookie() function in XenForo_Helper_Cookie:

public static function setCookie($name, $value, $lifetime = 0, $httpOnly = false, $secure = null)
    $expiration = ($lifetime ? (XenForo_Application::$time + $lifetime) : 0);
    return self::_setCookieInternal($name, $value, $expiration, $httpOnly, $secure);
My login form works perfectly if the checkbox for keep connected is checked, but if the checkbox isn't checked, doesn't work as I want. If isn't checked, the login works but the $lifetime isn't 0.

This is my code:

// In setCookie() I put 0, but apparently doesn't work.
if ($bRemember) {
else {
    $value = $dbUserModel->getUserRememberKeyForCookie($iUserID);
    if (!$value)
        return false;
    XenForo_Helper_Cookie::setCookie('user', $value, 0);


Well-known member
I think the question to check is when do you execute your code? XenForo probably overrides your settings by it's own internal functions when the session is created. In other words, it probably uses the same helper for the same cookie value but after you. To check this, just dump a fake variable in your code (ie:1) and in the method "setUserRememberCookie" from the class "XenForo_Model_User" (ie:2).
I suggest installing this add-on which will remove the Remember Me check box. All visitors will use cookies with this add-on installed.
This is not what I want. What I want is that if the checkbox is pressed, the lifetime is 0 seconds for the session to close automatically.

What is the resulting cookie lifetime with your code?
The resulting cookie is the same, press or not press the checkbox.