Function To Remove All Cookies For The Domain

This PHP function will delete all valid cookies for a domain. We’re also sanitizing the cookie variable names from invalid characters.

function clearAllCookies(){
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
        foreach($cookies as $cookie) {
            $parts = explode('=', $cookie);
            $name = trim($parts[0]);
            /*
            this removes any invalid cookie names preventing this warning:
            PHP Warning:  Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'
            */
            $name = str_replace(array("=", ",", ";", " ", "\t","\r","\n",chr(013),chr(014)), "", $name);
            if($name <> ''){
                setcookie($name, '', time()-1000);
                setcookie($name, '', time()-1000, '/');
            }
       }
}

Leave a Reply

Your email address will not be published. Required fields are marked *