Current User

The methods for retrieving the current user in client sites is slightly different from the Control Panel. Each method is as follows.

Control Panel

To retrieve the current user, use the following code.

Example 3.3. Retrieving the active user object

<?php 
    $user = Application::GetUser();
?>

This object will always correspond to a valid user with Control Panel access.

Client Site

On the client site, you're not always guaranteed to have a logged-in user. If the user is logged in, they will always belong to a custom user directory that belongs to the active client. If you try to retrieve the user and a user is not logged-in, an exception is thrown.

Example 3.4. Retrieving the active user

<?php
   try {
       $user = Application::GetUser();
       // user is logged-in
   }
   catch (Exception $ex) {
       // user is not logged-in
   }
?>

If the user is not-logged in, you can retrieve a unique hash that corresponds to the user (based on their IP address and web browser). This is an MD5 hash (32 characters long made up of hexadecimal characters). You can retrieve this hash using the following code.

Example 3.5. Retrieving the unique user hash.

<?php
    $hash = Application::GetUserHash();
?>

You can access this hash even if the user is logged-in but you typically won't need it. The following listing shows how you can retrieve the user value you need.

Example 3.6. Retrieving the user or the hash

<?php
    try {
        $user = Application::GetUser();
    
        // user is logged-in
        // do something with the user object
    }
    catch (Exception $ex) {
        $hash = Application::GetUserHash();
        // do something with the user hash
    }
?>