Forums

Authenticate Wordpress login from External PHP script? (5 posts)

  1. Brett2000
    Member
    Posted 9 months ago #

    Hi,

    I'm not sure WHY I cannot find this information. Basically I need to run several external PHP scripts on the same domain as my Wordpress blog. What I want to do is authenticate that the person running the external PHP script is currently logged in to Wordpress (at any level, I don't care which role they have).

    I don't need anything fancy, or even very secure, because I know my users are not tech savvy enough to hack through it. All I need is something simple that will look to see - is this person currently logged in to my Wordpress blog right now. Here is what I had, it did work for a few days, now it does not work??
    --------------------
    ini_set('display_errors', 1);
    require_once('../../../wp-config.php'); //Please make sure that the path is correct

    global $user_ID;

    $wp_user = new WP_User($user_ID);
    if (true == empty($wp_user) || $wp_user->ID < 1) {
    //In this case we are dealing with a visitor, not member
    wp_die("Would you please be so kind to log in?");
    }
    -------------------------

    Any ideas on why this used to work, and now it does not, or why it cannot work now? Any other ways I can do this? Every time I run it now I end up with $wp_user=NOT EMPTY, and ID=0, regardless of whether I am logged in to my Wordpress blog, or not? The only thing different, I changed themes, I did try changing back to the new theme, it did not fix the problem, so that might not be what caused this.

    Any help greatly appreciated, I don't know why this is so difficult, seems it would be a fairly straight-forward way to do it?

    Thanks,
    -Brett

  2. mmaaxx
    Member
    Posted 8 months ago #

    Did you found any info on that?

    Thanks.

  3. apljdi
    Member
    Posted 8 months ago #

    I think I'd check for the presence of WP's login cookies.

  4. lkoudal
    Member
    Posted 4 months ago #

    Hi

    I actually found the code on another blog, here it is:

    require('wp-blog-header.php');
    $user_login = 'admin';
    $user = get_userdatabylogin($user_login);
    $user_id = $user->ID;
    wp_set_current_user($user_id, $user_login);
    wp_set_auth_cookie($user_id);
    do_action('wp_login', $user_login);

    remember to change the location of the wp-blog-header.php file as well as the user_login variable.

    the code was found here:

    http://www.lbsharp.com/wordpress/index.php/2007/12/10/wordpress-auto-login/

  5. intellivision
    Member
    Posted 4 months ago #

    lkoudal, that will set the user to your admin user, no matter who the user really is. Be careful.

    Maybe that's what you want to do.

Reply

You must log in to post.

About this Topic