Code for getting WordPress logged user from external php file

We have used a Session variable for getting the WordPress logged in user id from an external php file.
But it didn’t work once after upgrading the wordpress. Obviously the session variable setting page was overwritten by new wordpress update.

So, we have decided to use the wordpress object to get the userid instead of session variable so that any future wordpress update won’t affect our code.

We have used below piece of code in the external php file, and it was working fine.

require_once('../wp-config.php');
require_once('../wp-includes/wp-db.php');
require_once('../wp-includes/pluggable.php');
global $current_user;
get_currentuserinfo();
$userid= $current_user->ID;
Posted in Php Tagged with: