0

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 "<script>window.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="example@gmail.com">
    <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.

0

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!

  • I entered your code but unfortunately it still doesn't work. – Matteo Feduzi Aug 30 at 12:36
  • What kind of issue you are facing ? is there any errors occurred ? try to print print_r($_POST); inside if (isset($_POST['submit'])). then check did you get posted data ? – Chetan Vaghela Aug 30 at 12:39
  • Even during the first visit (ie before sending the form and the "submit" button), it is returned true to the following code: if (isset ($ _ POST ['submit'])) { – Matteo Feduzi Aug 30 at 12:39
  • Effectively the array is printed without any content, namely: Array ([username] => [password] => [submit] => Login) – Matteo Feduzi Aug 30 at 12:41
  • But it is normal in the case of the first visit – Matteo Feduzi Aug 30 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.