0

When I run wp_signon () I have several warning similar to this: "Warning: Cannot modify header information - headers already sent by (output started at /web/htdocs/www.example.com/home/wp-includes/class.wp-styles.php:237) in /web/htdocs/www.mysite.com/home/wp-includes/pluggable.php on line 892" It do the login anyway, and if I change page then does not give me no problem, but I can not understand these errors. I am attaching the login code. Thanks a lot in advance for the help!

header.php :

    <div id="loginbar">

    <?php   $current_user = wp_get_current_user();
            if(isset($_POST["submit"])){        
            custom_login();
            }?>

    <?php if(!is_user_logged_in()): ?>  
        <form name="frontlog" method="post">            
            <input id= "log" type="text" name="log" placeholder="Username">     
            <input id= "pwd" type="password" name="pwd" placeholder="Password"> 
            <input type= "submit" name="submit" class="linkbuttonleft" id="loginbutton">    
        </form>     
        <a class="linkbuttonright" title="Registrati" href="http://www.example.com/registrazione/">
        <img id="chiave" src="http://www.example.com/wp-content/themes/BiscuitsTheme/immagini/chiave.png" onmousedown="return false"></a>   
    <?php else: ?>       
        <p> <?php echo 'Benvenuto ' . $current_user->user_login;?> </p>     
        <a class="linkbuttonleft" title="Logout" href="<?php echo wp_logout_url( home_url() ); ?>"></a>         
        <img id="lucchetto" src="http://www.example.com/wp-content/themes/BiscuitsTheme/immagini/lucchetto.png" onmousedown="return false"></a>
        <a class="linkbuttonright" title="Profilo" href="http://www.example.com/profilo/">
        <img id="profilo" src="http://www.example.com/wp-content/themes/BiscuitsTheme/immagini/profilo.png" onmousedown="return false"></a>
    <?php endif; ?>
</div>

functions.php :

    function custom_login() {
    $creds = array();   
    $creds = array(
        'user_login'    => $_POST["log"],
        'user_password' => $_POST["pwd"],
        'remember'      => true
    );

    $user = wp_signon( $creds, false );

    if ( is_wp_error($user) ) {
        echo $user->get_error_message();
    }
    else {
        wp_set_current_user( $user->ID, $user->name );
        wp_set_auth_cookie( $user->ID, true, false );
    }
};add_action( 'after_setup_theme', 'custom_login' );
  • It means server has already sent header info so either you have different header or echo function somewhere and then trying to redirect - after login. Check the files n line numbers in above error msgs. – user769889 Oct 02 '16 at 22:02
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Michael Gaskill Oct 02 '16 at 23:14

2 Answers2

0

the most obvious issue could be

<div id="loginbar">

<?php   $current_user = wp_get_current_user();
        if(isset($_POST["submit"])){        
        custom_login();
        }?>

The login function will load a new page, which php cannot do once there is data outputted to the page, you have 1 div above the function and I assume your header output as well.

Try to use from a functions file (theme or plugin) and use the init hook:

function cust_log(){
        $current_user = wp_get_current_user();
        if(isset($_POST["submit"])){        
        custom_login();
}
add_action('init', 'cust_log');
David
  • 5,897
  • 3
  • 24
  • 43
0

I have solved wp_signon() function for "first time no loggedIn error" so you just call set_current_user($user_verify->ID) function below wp_signon() function :

require('wp-load.php'); 
$err = '';
$success = '';
global $wpdb,$current_user;
    $response = array();
    $data = json_decode(file_get_contents("php://input"));

    $email    = $data->email_id;
    $password = $data->password;
    $username = $wpdb->escape($email);
    $password = $wpdb->escape($password);
    $remember = true;
    if($remember) $remember = "true";
    else $remember = "false";
    $login_data = array();
    $login_data['user_login'] = $username;
    $login_data['user_password'] = $password;
    $login_data['remember'] = $remember;

    $user_verify = wp_signon( $login_data, false ); 
    set_current_user($user_verify->ID);
    if (is_user_logged_in()) 
    {
        get_currentuserinfo();

            $response['user_id']=$user_verify->ID;
            $response['first_name']=$current_user->user_firstname ;
            $response['last_name']=$current_user->user_lastname;
            $response['email']=$current_user->user_email;
      $status="success";
      $msg="";
    } else {    
      $status="failed";
      $msg="Invalid Credential.";
     }
Dipak Kolhe
  • 23
  • 1
  • 3