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

From Web Services Wiki

Jump to: navigation, search



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


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
// 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
// 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.


Personal tools