|
Onto another example, substitute mod_blank.php with: <?php defined('_JEXEC') or die('Restricted access'); $user =& JFactory::getUser(); if ($user->guest) { echo "<p>hey stranger, you will always remain a stranger</p>"; } else { echo "<p>username: $user->username</p>"; echo "<p>user: $user->name</p>"; echo "<p>email: $user->email</p>"; } ?> JFactory is a fundamental Joomla class containing getter functions for various objects. One of the things it returns is a reference to the JUser object. If you don’t get what a reference is (or what this ampersand does) you need to google a bit (search for pass by reference). Let’s say for now that it returns a reference to the original User object and not just a copy of it, allowing us to modify its properties. There is much science involved behind the JFactory thing, we are just interested in using it, the code should look pretty straight forward. In the admin backend, under the site tab you can find the User Manager and create a user, see if this piece of code works or not, don’t take anything for granted. Have a look at the JUser class at the Joomla API reference, let’s play with it a bit more by iterating through the object's member properties. <?php defined('_JEXEC') or die('Restricted access'); $user =& JFactory::getUser(); print "\n Current User \n"; foreach ($user as $key => $value) { if (is_string($value)) { print "<p> user property $key is $value </p>\n"; } else { print "<p> user property $key is not a string </p>\n"; } } ?> Again, the code should be easy to follow, the foreach construct, praised by Perl, helps traversing arrays but object properties as well. You can see more about it here. The is_string function is needful here since not all JUser properties can be converted to strings to be printed out. As you would expect is_string returns a boolean value. You can see now how effortlessly we can get data about the current user, the Joomla API is very well written and provides plenty of methods for building upon. |