Title: Authenticate WordPress login from External PHP script?
Last modified: August 19, 2016

---

# Authenticate WordPress login from External PHP script?

 *  [brett2000](https://wordpress.org/support/users/brett2000/)
 * (@brett2000)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/)
 * 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

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [mmaaxx](https://wordpress.org/support/users/mmaaxx/)
 * (@mmaaxx)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/#post-996126)
 * Did you found any info on that?
 * Thanks.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/#post-996127)
 * I think I’d check for the presence of [WP’s login cookies](http://codex.wordpress.org/WordPress_Cookies).
 *  [Lars Koudal](https://wordpress.org/support/users/lkoudal/)
 * (@lkoudal)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/#post-996186)
 * 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/](http://www.lbsharp.com/wordpress/index.php/2007/12/10/wordpress-auto-login/)
 *  [intellivision](https://wordpress.org/support/users/intellivision/)
 * (@intellivision)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/#post-996188)
 * 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.
 *  [cwc](https://wordpress.org/support/users/cwc/)
 * (@cwc)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/#post-996215)
 * 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()) {
           ...
       }
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Authenticate WordPress login from External PHP script?’ is closed to 
new replies.

## Tags

 * [authentication](https://wordpress.org/support/topic-tag/authentication/)
 * [external](https://wordpress.org/support/topic-tag/external/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [script](https://wordpress.org/support/topic-tag/script/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 6 participants
 * Last reply from: [cwc](https://wordpress.org/support/users/cwc/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/authenticate-wordpress-login-from-external-php-script/#post-996215)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
