Using PHP to toggle stylesheet and header image, set in cookie

I know this is an old post and you have probably already found an answer, but here goes…

When setting cookies, you need to include it in the HTTP headers as explained here: http://php.net/manual/en/function.setcookie.php

This means that you need to add a WordPress action hook similar to as follows in your functions.php file (untested) before the headers are sent.

I have used ‘template_redirect’, but you could read into using ‘init’ also.

add_action( 'template_redirect', 'set_cookie');
function set_cookie() {
    // Set cookie 
    if (isset($_POST["styles"])) {
        setcookie("chosenStyle", $chosenStyle ,time()+31536000, "");
        wp_redirect( home_url() ); // Redirect back to home page 
        exit; 
    }
}

Have a read about WordPress actions/filters and this page:
http://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect