Programmatically logs a user in

I'm using multisite and trying to Programmatically log the new users in, I've successfully created users and their blogs, but got the errors here:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\ark\wp-includes\class.wp-scripts.php:128) in C:\xampp\htdocs\ark\wp-includes\pluggable.php on line 681

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\ark\wp-includes\class.wp-scripts.php:128) in C:\xampp\htdocs\ark\wp-includes\pluggable.php on line 682

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\ark\wp-includes\class.wp-scripts.php:128) in C:\xampp\htdocs\ark\wp-includes\pluggable.php on line 683

******
Here is my code:
function programmatic_login( $username ) {
if ( is_user_logged_in() ) {
wp_logout();
}
add_filter( 'authenticate', 'allow_programmatic_login', 10, 3 ); // hook in earlier than other callbacks to short-circuit them
$user = wp_signon( array( 'user_login' => $username ) );
remove_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
if ( is_a( $user, 'WP_User' ) ) {
wp_set_current_user( $user->ID, $user->user_login );
if ( is_user_logged_in() ) {
return true;
}
}

return false;
}

/**
* An 'authenticate' filter callback that authenticates the user using only the username.
*
* To avoid potential security vulnerabilities, this should only be used in the context of a programmatic login,
* and unhooked immediately after it fires.
*
* @param WP_User $user
* @param string $username
* @param string $password
* @return bool|WP_User a WP_User object if the username matched an existing user, or false if it didn't
*/
function allow_programmatic_login( $user, $username, $password ) {
return get_user_by( 'login', $username );
}

....
$user_id = wpmu_create_user( 'abc', $password, 'alex@site.com' );
$id = wpmu_create_blog( 'site.site.com', '/my55cest/', 'my first site here', $user_id);
programmatic_login('abc');

....