• Hey, I’ve been working on some PHP for a C# app I’m working on. The app has the user type in their username and password and then queries the server to see if they have permissions to do stuff. I got the C# part to work, and the PHP works… sort of. It won’t find users. I’ve tried get_user_by(‘login’, $username), username_exists($username), and even SQL queries, but it’s not working. Hopefully you can help me out. This is what I’ve got (couldn’t get code to work here):

    https://pastebin.com/AbWSuq26

Viewing 15 replies - 1 through 15 (of 18 total)
  • popper

    (@julialasarte)

    I tested your script locally and it works just fine. $_POST is probably not receiving what you expect.

    Thread Starter snoozbuster

    (@snoozbuster)

    Hmm… I added the line echo $u . " " . $p . " " right after $p = $_POST['p'], and it’s echoing back the correct username and password. I could show you my C# code if you like, dunno if you know C# or not but maybe you’ll see something. The data I’m POSTing looks like "u=username&p=password".

    Thread Starter snoozbuster

    (@snoozbuster)

    I suppose it could be worth noting that the ContentType is "application/x-www-form-urlencoded", but I suppose that’s evident from the type of data I’m POSTing.

    popper

    (@julialasarte)

    Sure, post it to paste bin, I’m guessing .net?

    Other than that, you’ve probably done this already but, have you tried sending the user directly to the function?

    get_user_by('login', 'username')

    Thread Starter snoozbuster

    (@snoozbuster)

    Yeah, .NET. I was using one method, and then I found a quicker way, but neither work. I’ll post both. I just tried passing ‘test’ directly, but it did the same thing. I’m really at a loss here.

    Here’s the C# code, but I don’t know if it’ll help. I found a test .NET POST and GETter, but it did the same thing. https://pastebin.com/SprV69UP

    Thread Starter snoozbuster

    (@snoozbuster)

    Sorry, use this one. https://pastebin.com/HMmVRpG1

    popper

    (@julialasarte)

    Ok, the C# code looked perfectly fine. I tested it anyway and it works as it should both in my local wordpress installation and a remote one.

    As you can see, the code I used is almost identical to yours so the problem is definitely somewhere else.

    Are you 100% sure that user login exists in the database?

    Thread Starter snoozbuster

    (@snoozbuster)

    I am 100% sure the user exists. I just checked, and there is a user named “test” (what I’ve been using) with the username “test” and the nickname “test” on my users panel in the backend. I’ve tried it with a couple admin accounts, too, and the same thing happens. I have to wonder if, for some reason, the database is empty because I’m running the file outside WordPress…? At this point, it has to be something like the database is empty for whatever reason.

    popper

    (@julialasarte)

    That shouldn’t be it, since I’ve been trying your code outside wordpress -even contacting a remote host- and it works fine, but just to be sure, you can paste the function inside functions.php of your theme and trying it there.

    Other than that, have you tried getting the user by id or email?

    Thread Starter snoozbuster

    (@snoozbuster)

    I did try by email. So, then, the issue is likely with something in my site’s configuration?

    popper

    (@julialasarte)

    Maybe? At this point, it’s probably something incredibly silly we’re not noticing. Have you tried saving the query wp generates and pasting that directly into phpmyadmin?

    Thread Starter snoozbuster

    (@snoozbuster)

    This is the only query that looks relevant…

    [12] => Array
            (
                [0] => SELECT * FROM wp_users WHERE user_login = 'admin'
                [1] => 0.000102996826172
                [2] => require, require_once, require_once, require_once, WP->init, wp_get_current_user, get_currentuserinfo, wp_validate_auth_cookie, get_user_by, WP_User->get_data_by
            )

    I’m trying to install phpMyAdmin right now, but I don’t know how well it’s going to work (or even what I’m looking for).

    popper

    (@julialasarte)

    Looks perfectly normal. Yeah, you don’t even need phpmyadmin, just run the query directly to see if you get any results, you can probably use ssh for a remote host or terminal/console for local.

    Thread Starter snoozbuster

    (@snoozbuster)

    Pretty sure I did this already but it didn’t work… I’ll try it again, though. Nope, not working. I replaced the get_user_by line with this:
    $user = $wpdb->get_var("SELECT * FROM $wpdb->wp_users WHERE user_login = 'test'",0,0); and it’s still not working.

    Thread Starter snoozbuster

    (@snoozbuster)

    And before you say I didn’t use a semicolon, I tried it with and without.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘get_user_by not returning any users.’ is closed to new replies.