If you are writing PHP code in Drupal and would like to access basic user information (e.g. UID, username), you can simply define the global variable $user and get the data from there:
1 2 3 4 5 |
<?php global $user; echo $user->uid . "\n"; // Print out the user ID echo $user->name . "\n"; // Print out the username ?> |
However, it would be more complicated when you want to access the same information outside of Drupal system (but still in the same domain). For instance, you want to implement a PHP page to provide data to your JavaScript (AJAX or Web API style), and you want to have access to the current Drupal user directly inside your PHP code instead of passing the UID around (which is quite insecure in my opinion).
My problem is briefly demonstrated in the diagram below: