WordPress.org

Forums

Authenticate WordPress login from External PHP script? (6 posts)

  1. Brett2000
    Member
    Posted 6 years 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 6 years ago #

    Did you found any info on that?

    Thanks.

  3. s_ha_dum
    Member
    Posted 6 years ago #

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

  4. Lars Koudal
    Member
    Posted 6 years 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. Matt
    Member
    Posted 5 years 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.

  6. cwc
    Member
    Posted 5 years ago #

    I did the following:

    Add the following in your wp-config.php:

    define('COOKIE_DOMAIN', 'yourdomain.org');
    define('COOKIEPATH', '/');

    You can make the path more specific (e.g. if your external script is in /myscript/ and your WP installation is in /myscript/wordpress/, then you can use '/myscript/' as COOKIEPATH).

    And then in my external script:

    require_once('wp-pos/wp-blog-header.php');
    if (! is_user_logged_in()) {
        ...
    }

Topic Closed

This topic has been closed to new replies.

About this Topic