1
votes

When I set a cookie in a Wordpress plugin, I get the "headers already sent" error. Can someone suggest how I can fix this?

Warning: Cannot modify header information – headers already sent by (output started at /home/content/53/7742253/html/wordpress/wp-content/themes/twentyten/header.php:11) in /home/content/53/7742253/html/wordpress/wp-includes/pluggable.php on line 692,693,694

my code:-

     if(isset($_REQUEST['id']) && !is_user_logged_in())

      {

         require_once( ABSPATH . WPINC . '/registration.php' );


    $registration = get_option( 'users_can_register' );

       global $wpdb;

       $user_id = $wpdb->get_var( $wpdb->prepare("SELECT user_id FROM              

                  $wpdb->usermeta           WHERE 

                meta_key = 'id' AND meta_value =%s",$id) );

         if ( empty($user_id) ) 
              {

                 $wp_user_obj = get_user_by('email', $email);

                $user_id = $wp_user_obj->ID;

               }

               if ( $user_id)

                  {

                   wp_set_auth_cookie( $user_id );

                      wp_set_current_user( $user_id );

               if ( isset( $_REQUEST['redirect_to'] ) && !empty( 
           $_REQUEST['redirect_to'] ) ) 
            {

              wp_redirect( home_url() );

                 }
                 else 
               {

                wp_redirect(home_url() );

                 }

                   }      

                     else

                   {

                   $userdata = array();


                $user_id = wp_insert_user($userdata );

                 wp_new_user_notification($user_id,$user_pass);

                  if ( $user_id )
                     {

                         $creds = array();

                        $creds['user_login'] = $username;

                         $creds['user_password'] = $user_pass;

                          if ( !empty( $remember ) )
                            { 

                             $creds['remember'] = true;

                                 }

                    $user = wp_signon( $creds, true );  

                     update_usermeta( $user_id,'id',esc_attr( $_REQUEST['id']));

                 update_usermeta( $user_id,'fname',esc_attr( $_REQUEST['fname']));

                   update_usermeta( $user_id,'lname',$_REQUEST['lname']);

              update_usermeta( $user_id, 'email',esc_attr( $_REQUEST['email']) );

                 wp_redirect(home_url() );

               }

                }

               }    

             global $user_ID; $user = get_userdata( $user_ID );

           if(is_user_logged_in())
         {

                echo $user->user_login ; 

              } 



and heder.php line:-  `<html <?php language_attributes(); ?>>`
2
Make sure that there is no kind of output before setting the cookie, maybe a blank line or another module already outputting something. - Quasdunk
I wonder if we have some sort of FAQ for the PHP Headers already sent scenario caused by using header, session or set_cookie. - hakre

2 Answers

2
votes

You're calling your set_cookies function too late. At the time you call it, output has already started. You need call it before any output has started, because cookies can not be set if output has already started (see as well: setcookieDocs).

In your case before header.php line 11 is being executed.

That's merely before the theme get's loaded.

1
votes

You can avoid header modification warning messages by buffering your source, before it makes it to the browser. If you do not have access to php.ini you can add php_value output_buffering 4096 to your .htaccess or you can add ob_start("ob_gzhandler"); at the top and ob_end_flush() at the bottom of your wordpresses root index.php