Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

php - Redirect after login, Wordpress URLs

I'm using the following function to redirect a user after a failed login attempt. In functions.php, I added:

add_action( 'wp_login_failed', 'inline_login_fail' );

function inline_login_fail( $username ) {
    $referrer = $_SERVER['HTTP_REFERER'];
    if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
        if ( !strstr($referrer,'?login=failed') ) { // don’t append twice
        wp_redirect( $referrer . '?login=failed' ); 
        } else {
        wp_redirect( $referrer );
        }
    exit;
    }
}

This works, but the problem is I can't figure out how to parse the '?login-failed'. When I asked this as a PHP question, they said the URL is malformed.

My URL: www.site.com/?page_id=57?login=failed

Preferred URL: www.site.com/?page_id=57&login=failed

Now you would think I could just change the '?' to '&'. When I do this, it creates an issue if the user is trying to login from the home page and fails. It creates a URL which is a 404: "The requested URL /wordpress/&login=failed was not found on this server."

Is there any way to correct this function so it will work with the preferred URL? That's so I can retrieve the value of whether login has failed, like this:

<?php
if(isset($_GET['login']) === true and $_GET['login'] === 'failed')
{
  get_template_part( 'login', 'failed' );
} else {
  get_template_part( 'login', 'form' );
}
?>

Otherwise, I can't figure out how to work with that URL at all. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

check for ?

<?php
    function inline_login_fail( $username ) {
        $referrer = $_SERVER['HTTP_REFERER'];
        if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
            if ( !strstr($referrer,'login=failed') ) { // don’t append twice
                if(!strstr($referrer, '?')){
                    wp_redirect( $referrer . '?login=failed' ); 
                } else {
                    wp_redirect( $referrer . '&login=failed' ); 
                }
            } else {
                wp_redirect( $referrer );
            }
        exit;
        }
    }
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...