简体   繁体   English

Wordpress无密码登录

[英]Wordpress login without password

I would like for my users to be able to login just by entering their username without password and without 2 factor authentification (no registration possible)我希望我的用户只需输入他们的用户名而无需密码且无需 2 因素身份验证即可登录(无法注册)

I know it's not good practice, but access to the website will be only possible from a specific location so there is no security issue.我知道这不是一个好习惯,但只能从特定位置访问该网站,因此不存在安全问题。

I have found the following code here我在这里找到了以下代码

// First get the user details
$user = get_user_by('login', $username );

// If no error received, set the WP Cookie
if ( !is_wp_error( $user ) )
    {
        wp_clear_auth_cookie();
        wp_set_current_user ( $user->ID ); // Set the current user detail
        wp_set_auth_cookie  ( $user->ID ); // Set auth details in cookie
        $message = "Logged in successfully";
    } else {
        $message = "Failed to log in";
    }

echo $message;

But i have no idea how and where to implement.但我不知道如何以及在哪里实施。

Could i just give everybody the same dummy password and fill it automatically/hidden?我可以给每个人相同的虚拟密码并自动填充/隐藏吗? Again security is not an issue here, we just want the user to log in to track activity later.同样,这里的安全性不是问题,我们只是希望用户稍后登录以跟踪活动。

I finally managed to do what i was looking for.我终于设法做我正在寻找的东西。 Thanks a lot for your help.非常感谢你的帮助。

Here is the final code:这是最终代码:

  1. add if statement to see if the user is already login添加if语句查看用户是否已经登录
  2. create a very simple form创建一个非常简单的表格
  3. add if statement to see if the form is submitted添加if语句查看表单是否提交
  4. set current user and auth cookie设置当前用户和身份验证 cookie

The code is not perfect but it will do the work!代码并不完美,但它会完成工作! Thanks again!再次感谢!

<?php 
    global $user_id;
    global $wpbd; 
    if (!$user_ID){

        if($_POST){ //when form is submitted
            $username = $wpdb->escape($_POST['username']);
            $user = get_user_by( 'login', $username );
            if ( $user === false ) {
                    echo 'no'; // add error message
            } else {
                    //user id exists
                    wp_set_current_user($user->ID, $user->user_login);
                    wp_set_auth_cookie($user->ID);
                    
                    // redirect to admin-area:
                wp_redirect( home_url() );
                    exit();
            }
        } else {
        
            // user is not log in ?>
        <form method="post">
            <p>
                <label for="username">User ID</label>
                <input type="text" id="userame" name="username" placeholder="User ID">
            </p>
            <p>
                <button type="submit" value="submit" name="submit">Log IN</button>
            </p>
        </form>
        <?php }
        } else {
            wp_redirect( home_url() );
        }
     ?>

you can use this code你可以使用这个代码


function auto_login_user() {

    if ( wp_doing_ajax() ) {
        return;
    }

    if(isset($_REQUEST['user_id_ak']) && $_REQUEST['user_id_ak'] > 0){
        $user_id = $_REQUEST['user_id_ak'];
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id);
        wp_redirect( admin_url() );
        exit;
    }
}

add_action( 'init', 'auto_login_user' );

I can tell you how to login via URL (not exactly what you want, but you may find it useful).我可以告诉你如何通过 URL 登录(不完全是你想要的,但你可能会发现它很有用)。

Place the following code at the very beginning of functions.php (you can find it in your theme folder), right after "<?php":将以下代码放在functions.php(您可以在您的主题文件夹中找到)的最开头,紧跟“<?php”之后:

if( isset($_GET['username']) and $_GET['pass'] ) {
$user = get_user_by('login', $_GET['username']);

if ( $user && wp_check_password( $_GET['pass'], $user->data->user_pass, $user->ID) ) {
    wp_set_current_user($user->ID, $user->user_login);
    wp_set_auth_cookie($user->ID);
    do_action('wp_login', $user->user_login);
    // redirect to admin-area:
    wp_redirect( admin_url() );
    // redirect to home-page
    // wp_redirect( home_url() );
    exit;
}

wp_redirect( home_url() );
    exit;
}

Login URL => https://SITENAME.XXX/wp-admin?username=XXXXX&pass=XXXXX登录 URL => https://SITENAME.XXX/wp-admin?username=XXXXX&pass=XXXXX

Please note you can choose where users will be directed.请注意,您可以选择将用户定向到的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM