Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Drupal 7 get information of current logged-in user in external PHP file

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 552
    Comment on it

    Hi,

    Here is a code for loading current logged-in user using Bootstrap full in external PHP file.

    function user_load_from_bootstrap($uid = NULL) {
    define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
    $base_url = 'https://'.$_SERVER['HTTP_HOST']; // this will load base url of website
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    global $user;
    return $user;
    }
    

    without loading bootstrap and Using Session for current logged in user:

    function user_load_from_session($uid = NULL) {
    $session_name = session_name();
      if (!empty($session_name)) {
        if ($uid = db_query("SELECT uid FROM {sessions} WHERE sid = :sid", array(':sid' => $_COOKIE[$session_name]))->fetchColumn()) {
          $user = user_load($uid);
          return $user;
        }
      }
    }
    

    Hope this may be helpful to for drupal developers.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: