How to get the currently logged-in WebAuth user with the Stanford Web Application Toolkit

From Web Services Wiki

Jump to: navigation, search

Contents

Problem

You want to get information about the currently logged-in user in a Webauth-protected script.

Solution

StanfordApp provides a method to get the currently logged-id Webauth user.

Call get_visitor

Use the get_visitor method of StanfordApp to get the current user.

// Include StanfordApp
include_once("stanford.app.php");
 
// Initialize StanfordApp
$app = new StanfordApp();
 
// Check if behind Webauth
if($app->auth->is_behind_webauth()) {
 
  // Get the current visitor (returns a StanfordPerson object)
  $person = $app->get_visitor();
 
  // Echo their SUNetID
  echo "<p>Welcome to our website, " . $person->get_sunetid() . "</p>";
}
else {
  echo '<p>Not behind Webauth, cannot get currently logged-in user</p>';
}

Another way to get the current visitor is to call StanfordPerson::get_current_user, as shown below.

// Include StanfordPerson
include_once("stanford.person.php");
 
// Get current user
$person = StanfordPerson::get_current_user();
 
// $person now contains a StanfordPerson object (or null if nobody is logged in)

Accessing information about the user

To access more information contained within the $person object, including the user's name, e-mail address, and affiliations, read How to get information about users in the directory with the Stanford Web Application Toolkit.

References

Personal tools