Check for $ _POST fields in a POST method form

I'm trying to check if the username and password fields are present in the login form.

The problem is that the if (!empty ($_POST)) code does not do this to me.

UPDATE:

if (isset($_POST['submit-btn'])) {

    //Sanitize POST Array
    $POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

    $username = $POST['username'];
    $password = $POST['password'];

    if(empty($username)) {
        echo "Username o email non inserita"; 
        return false;
    }

    if(empty($password)) {
        echo "Password non inserita"; 
        return false;
    }

    if($username  $password) {

        $login_array = array();
        $login_array['user_login'] = $username;
        $login_array['user_password'] = $password;

        $verify_user = wp_signon($login_array, true);
        if (!is_wp_error($verify_user)) {
            echo "scriptwindow.location = '" . site_url() . "'/script";
        } else {
            echo 'Credenziali non valide!';
        }
    }
}

This is instead the login form that is on the same page, that is on the my-login.php page:

form id="wp_login_form" action="/login/" method="post"
    label class="my-username" Username o Email/label
    input type="text" name="username" class="info-login" value="" placeholder="[email protected]"
    label class="my-password" Password/label
    input type="password" name="password" class="info-login" value="" placeholder="******"
    label 
    input class="myremember" name="rememberme" type="checkbox" value="forever"span class="hey"Remember me/span
    /label 
    button type="submit" id="submitbtn" name="submit-btn" value="Login"Login/button
/form

Thanks a lot to those who will help me. Best regards.

Topic wp-login-form mysql login Wordpress

Category Web


if you want to check username and password fields, try below code

if (isset($_POST['submit-btn'])) {
    $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
    if(empty($username))
    {
        echo "Username empty"; return false;
    }
    if(empty($password))
    {
        echo "Password empty"; return false;
    }

    if($username && $password)
    {
        $login_array = array();
        $login_array['user_login'] = $username;
        $login_array['user_password'] = $password;

        $verify_user = wp_signon($login_array, true);
        if (!is_wp_error($verify_user)) {
            echo "<script>window.location = '" . site_url() . "'</script>";
        } else {
            echo 'Credenziali non valide!';
        }
    }
}

let me know if this works for you!

About

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