Front end login form not working on live site

I am using the front end form submission. It works on localhost perfectly, but it's not working when my site is live. It shows the "success message", but originally it's not logged in. This is my code:

if ( 'POST' == $_SERVER['REQUEST_METHOD']  !empty( $_POST['action'] )  $_POST['action'] == 'log-in' ) :    
    global $error;
    $login = wp_login( $_POST['user-name'], $_POST['password'] );
    $login = wp_signon( array( 'user_login' = $_POST['user-name'], 'user_password' = $_POST['password'], 'remember' = $_POST['remember-me'] ), false );

    if (!$error) {
        save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );
        unset($_POST);
        wp_redirect( get_permalink( 4 ) );  
    }
    // wp_redirect( home_url() );
endif;

So what can I do to fix this?

Topic front-end Wordpress

Category Web


You need to set cookie, I modified your code as below:

Updated

<?php
function parse_user($info = null, $return = 'object') {
    if ( is_null( $info ) ) {
        global $current_user;
        if ( empty( $current_user->ID ) ) return null;
        $info = get_userdata( $current_user->ID );
    }
    elseif ( empty( $info ) ) {
        return null;
    }
    if( $return == 'ID' ) {
        if ( is_object( $info ) ) return $info->ID;
        if ( is_numeric( $info ) ) return $info;
    }
    elseif( $return == 'object' ) {
        if ( is_object( $info ) && $info->ID) return $info;
        if ( is_object( $info )) return get_userdata( $info->ID );
        if ( is_numeric( $info ) ) return get_userdata( $info );
        if ( is_string( $info ) ) return get_userdatabylogin( $info );
    }   
    else {
        return null;
    }
}

function wp_login_user_po9856($username, $password, $remember) {
    // Get the user based on the username from the POST
    $user = parse_user($username);

    // Remove html tags from the variables
    $username = strip_tags($username);
    $password = strip_tags($password);

    // Validate the Form Data
    if(!wp_check_password( $password_stripped, $user->user_pass ) ) return new WP_Error('incorrect_password', "Wrong Password!");

    // User login
    $login = wp_signon(array('user_login' => $username, 'user_password' => $password, 'remember' => (bool)$remember));
    if (is_wp_error($login))
        return $login;

    // Unset
    unset($_POST);

    // Custom Message
    save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );

    // Redirection
    wp_redirect(get_permalink( 4 ));
}
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && @$_POST['action'] == 'log-in')
{
    wp_login_user_po9856($_POST['user-name'], $_POST['password'], $_POST['remember-me']);
}

?>


Do not use wp_login function it is deprecated , instead use wp_signon. wp_signon function returns WP_Error on failure so use it to evaluate condition as following.

<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == 'log-in' ) :

$login = wp_signon( array( 'user_login' => $_POST['user-name'], 'user_password' => $_POST['password'], 'remember' => $_POST['remember-me'] ), false );

if ( !is_wp_error($login) ){ 
save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );
unset($_POST);
wp_redirect( get_permalink( 4 ) );
}

endif;
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.