【问题标题】:Error on Wordpress wp_signon() functionWordpress wp_signon() 函数出错
【发布时间】:2016-10-02 21:51:09
【问题描述】:

当我运行 wp_signon () 时,我有几个类似这样的警告: “警告:无法修改标头信息 - 标头已由 /web/ 中的(输出开始于 /web/htdocs/www.example.com/home/wp-includes/class.wp-styles.php:237)发送htdocs/www.mysite.com/home/wp-includes/pluggable.php 第 892 行" 无论如何它都会登录,如果我改变页面那么不会给我带来任何问题,但我无法理解这些错误。 我附上登录代码。 非常感谢您的帮助!

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' );

【问题讨论】:

  • 这意味着服务器已经发送了标头信息,因此您在某处有不同的标头或回显功能,然后尝试重定向 - 登录后。检查上述错误消息中的文件 n 行号。

标签: php wordpress login header


【解决方案1】:

最明显的问题可能是

<div id="loginbar">

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

登录功能将加载一个新页面,一旦有数据输出到页面,php 就无法执行此操作,您在该功能上方有 1 个 div,我也假设您的标题输出。

尝试从函数文件(主题或插件)中使用并使用 init 钩子:

function cust_log(){
        $current_user = wp_get_current_user();
        if(isset($_POST["submit"])){        
        custom_login();
}
add_action('init', 'cust_log');

【讨论】:

    【解决方案2】:

    我已经解决了“第一次没有登录错误”的 wp_signon() 函数,所以你只需在 wp_signon() 函数下面调用 set_current_user($user_verify->ID) 函数:

    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.";
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多