Forums | Mahara Community

Developers /
User object


Osbel Rondón's profile picture
Posts: 23

10 May 2017, 6:43

Hello everyone
In an implementation I am developing I am trying to get the user object to see its properties as username and email, but this object is empty (still having some user logged in).
Here I write my code to see if anyone can correct me or guide me in the right way.

global $USER;
echo empty($USER)?"empty":"dont empty";       //print "empty"
echo "<br>";
echo $USER->get('email');                     //print "Call to a member function get() on a non-object"
echo "<br>";
echo $USER->get('username');             //print nothing

Robert Lyon's profile picture
Posts: 773

30 May 2017, 9:38

Hi Osbel,

To test if the $USER object is working right you can look at this bit of test code: 

<?php

define('INTERNAL', 1);
define('PUBLIC', 1);
require('init.php');

function get_test_info() {
    global $USER;
    return array(
       'name'       => 'test',
       'plugintype' => 'core',
       'pluginname' => 'test',
       'elements'   => array(
           'id' => array(
               'type'         => 'html',
               'title'        => 'ID',
               'value' => $USER->get('id'),
           ),
           'username' => array(
               'type'         => 'html',
               'title'        => 'Username',
               'value' => $USER->get('username'),
           ),
        ),
    );
}
$form = pieform(get_test_info());

$smarty = smarty();
$smarty->assign('form', $form);
$smarty->display('form.tpl');

?>

I created this as htdocs/test.php and it will either display the id/username of the user that is logged in or id = 0 if logged out.

Hopefully that helps with diagnosing your problem

Cheers

Robert

2 results