automatic login to wordpress dashboard from another site

前端 未结 2 1084
你的背包
你的背包 2020-12-15 01:53

I want to log in automatically to WP admin/dashboard from another site without going thru the login process.. I\'ve tried the following but with no luck:

<         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 02:54

    If you have access to the files of the website where you trying to login. You could add a auto login php script and $_POST the username and password to this script, example:

    if ($_POST) {
    
        $errors = array();
    
        $username = esc_sql($_REQUEST['username']);
        $password = esc_sql($_REQUEST['password']);
        $remember = esc_sql($_REQUEST['rememberme']);
        $remember = ($remember) ? "true" : "false";
    
        $login_data = array();
        $login_data['user_login'] = $username;
        $login_data['user_password'] = $password;
        $login_data['remember'] = $remember;
        $user_verify = wp_signon($login_data, true);
    
        if (is_wp_error($user_verify)) {
            $errors[] = 'Invalid username or password. Please try again!';
        } else {
            wp_set_auth_cookie($user_verify->ID);
            wp_redirect(admin_url());
            exit;
        }
    
    }
    

    Wordpress codex references:

    • Login function: http://codex.wordpress.org/Function_Reference/wp_signon
    • Set cookie for admin login http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie
    • Get the admin page url: http://codex.wordpress.org/Function_Reference/admin_url

    Hope it helps.

    Edit: $wpdb->escape is deprecated since Wordpress version 3.6, use wpdb::prepare() or esc_sql() instead! I've changed the code to use esc_sql().

    • esc_sql(): http://codex.wordpress.org/Function_Reference/esc_sql

提交回复
热议问题