0

How i can make login where the parameter and the value is i get from meta_key and meta_value where have same post_id.. i ve been try, but nothing work.

here my code now :

//* 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;
}

and this my table :

enter image description here

have someone help me how i can solve my problem ? or can someone give me solution for me ? because now iam stuck now..

0

To log a user in you really only need the user's ID. Based on your info you should be able to obtain it from the username field.

// 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 );

On a non-login page you could probably refresh right after;

header("Refresh:0");
exit();

Now that the hard part is out of the way, it comes down to where you're getting your username from.

Most people don't store username/password in plaintext on post_metadata. But even then, and you're making a whole loop query for login data, you're still going to have to settle on logging in a single user. So the whole $data['result'][]= doesn't make a lot of sense.

  • iam sorry sir, iam still confuse how to apply thats condition to my code, sir may u make it more simple or can u do in my code to ? i think its will more help me thank you sir – Adam Projo Oct 25 '16 at 6:04

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.