使用元密钥和元值登录Api

时间:2016-10-25 作者:Adam Projo

如何进行登录,其中参数和值是我从meta\\U key和meta\\U value获取的,其中具有相同的post\\U id。。我一直在努力,但没有任何效果。

下面是我的代码:

//* Login Driver *//
$command = $_GET[\'command\'];
switch ($command) {
    case \'login_driver\':
    if(empty($_REQUEST[\'email\']) OR empty($_REQUEST[\'password\'])) {
           $data = array( "api_status" => 0, "api_message" => "Wrong Email Or Password");
           echo  json_encode($data);
        } elseif( $_REQUEST[\'email\'] == get_post_meta(get_the_ID(), \'email\', true) AND $_REQUEST[\'password\'] == get_post_meta(get_the_ID(), \'password\', true) ) {
           $loop = new WP_Query( 
                array(
                        \'post_type\'    => \'drivers\',
                        \'meta_query\'    => array(
                            \'relation\'      => \'AND\',
                            array(
                                \'key\'       => \'username\',
                                \'value\'     => $_REQUEST[\'email\'],
                                \'compare\'   => \'=\'
                            ),
                            array(
                                \'key\'       => \'password\',
                                \'value\'     => $_REQUEST[\'password\'],
                            )
                        )
                    )
                ); 
            $data = array("api_status" => 1, "api_message" => "success", "result" => "");
            if( $loop->have_posts() ) :
                while($loop->have_posts()) : $loop->the_post();
                    $data[\'result\'][] = array(
                        "id"        => get_the_ID(),
                        "post_name" => get_the_title(),
                        "username"  => get_post_meta(get_the_ID(), \'username\', true),
                        "password"  => get_post_meta(get_the_ID(), \'password\', true),
                        "email"     => get_post_meta(get_the_ID(), \'email\', true),
                        "phone"     => get_post_meta(get_the_ID(), \'phone\', true),
                    );
                endwhile;
            endif;
            echo  json_encode($data);
        }elseif($_REQUEST[\'email\'] != get_post_meta(get_the_ID(), \'email\', true) OR $_REQUEST[\'password\'] != get_post_meta(get_the_ID(), \'password\', true)){
           $data = array( "api_status" => 0, "api_message" => "Wrong Email Or Password");
           echo  json_encode($data);
        }else{
            $data = array( "api_status" => 0, "api_message" => "Wrong Email Or Password");
           echo  json_encode($data);
        }
break;
}
这是我的桌子:

enter image description here

有人帮我解决问题吗?或者有人能为我提供解决方案吗?因为现在我被困了。。

1 个回复
SO网友:jgraup

要登录用户,实际上只需要用户的ID. 根据您的信息,您应该能够从username 领域

// Get User object
$user = get_user_by( \'login\', $username );

// Clear any auth cookies
wp_clear_auth_cookie();

// Log the user in
wp_set_current_user( $user->ID );
wp_set_auth_cookie( $user->ID );
在非登录页面上,您可能会立即刷新;

header("Refresh:0");
exit();
既然困难的部分已经解决了,那就要看你的username 从…起

大多数人不储存username/password 以明文形式打开post_metadata. 但即便如此,如果您要对登录数据进行一个完整的循环查询,您仍然必须满足于只登录一个用户。所以整个$data[\'result\'][]= 没有什么意义。