Introduction of PHP COOKIE methods for timely effectiveness

  • 2021-01-03 20:51:39
  • OfStack

In general,php requires 1 swipe of the browser to appear cookie, so how to make cookie effective in time? The following is a practical method to make cookie effective in time. The code is as follows:

/**
 *  Set up the cookie
 * @param string $name  Key name 
 * @param mixed $value  value 
 * @param int $expire  Expiration time , The default is 1 day 
 */
public final function setCookie($name, $value, $expire = null){
    //cookie Value is empty , exit 
    if(empty($value)) return;
    // Expiration time 
    if(empty($expire)) $expire = time() + 86400;
    $_COOKIE[$name] = $value;
    // judge value Is it an array 
    if(is_array($value)){
        foreach ($value as $k => $v){
            if(empty($v)) continue;
            setcookie($name . "[$k]", $v, $expire);
        }
    }else{
        setcookie($name, $value, $expire);
    }
} 

Related articles: