Sometimes a wordpress plugin is not the proper solution for an complex application. You might want something to work alongside wordpress instead. One of the nice features of Worpress is the user managament. The WordPress documentation for this is non-existant as far as I ca tell, so here’s how you can quickly have your software find out if a user is logged in outside of WordPress.
WordPress stores the user’s login status in a cookie named “wordpress_logged_in_COOKIEHASH,” Where COOKIEHASH there is an MD5 hash of the ‘Site URL’ option. Line 421 of wp-settings.php:
define('COOKIEHASH', md5(get_option('siteurl')));
So you can use that to check the cookie, or you can copy it from an existing cookie on your machine, or generate it in your own cookie name. First, log yourself out of WordPress, because we are going to be changing the logged in cookie name, and you won’t be able to log in or out with the old cookie name. Now we can define a new cookie name in the wp-config.php file by adding the following line somewhere in the file:
define('LOGGED_IN_COOKIE', 'my_cookie_name');
For Reference The default is defined in wp-settings.php on line 392 as:
define('LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH);
So now I’ll always know what the cookie name is, so I can extract the username from the cookie value.
The value contains the following info:
username|doubleHashedPassword
Now that we have a username, we can fetch user data, such as a user_id. Something like this should do the trick assuming that you have established your DB connection:
if (!empty($_COOKIE[my_cookie_name])) {
// The user is logged in, so retrieve user data
$userArr=explode('|',$_COOKIE[my_cookie_name]);
$user_login=$userArr[0];
$sql="SELECT ID FROM wp_users WHERE user_login LIKE '$user_login'";
$query=mysql_query($sql);
$resultArray=mysql_fetch_array($query);
$user_id=$resultArray[ID];
} else {
// The user is not logged in so do something else
}
?>
That should work.
October 2nd, 2011 at 10:59 am
[...] = 'wpp-256'; var addthis_config = {"data_track_clickback":true}; View post : Getting Other Applications to Work with WordPress Users | Lacquer Head Random Bookmarks:apfelblick Die Gedanken sind freiApple – Support – Mac OS X [...]