简体   繁体   中英

Joomla 2.5.9, PHP 5.3.22, JFactory, Get User

I am using the Joomla Easy Flash Uploader and there is an auto email that is sent out to the administrator (or person of my choosing) once an upload is made. It is designed to show the logged in user's name in the email, however it simply keep showing "Guest" even though the form is only being accessed by logged in users. I desperately need this to properly pull the username. Here is how the message reads: (file name and location have simply been removed to protect my website.

A guest (Guest) has successfully uploaded FILE NAME to LOCATION at Tue, 12 Mar 2013 14:56:07 -0500.

  • Check out the code:

//email notification if ($_POST['notify'] != false && JMailHelper::isEmailAddress($_POST['recipient'])) { //fetch the mail object $mailer =& JFactory::getMailer();

    //set up the sender
    $config =& JFactory::getConfig();
    $sender = array( 
        $config->getValue( 'config.mailfrom' ),
        $config->getValue( 'config.fromname' )
    );
    $mailer->setSender($sender);

    //set up recipient
    $mailer->addRecipient($_POST['recipient']);

    //get user info
    $user_info = array();
    $user =& JFactory::getUser();
    if ($user->guest == true)
    {
        $user_info['name'] = 'A guest';
        $user_info['username'] = 'Guest';
    }
            else
    {
        $user_info['name'] = $user->name;
        $user_info['username'] = $user->username;
    }

    //set up message
    $body = $user_info['name']." (".$user_info['username'].") has successfully uploaded ".$fileName;
    $body.= "(".sizeToText($fileSize).") to ".JPATH_SITE.DS.$cleanedSubPath;
    $body.= " at ".date("r", time()).".\n";
    $mailer->setSubject('New File Uploaded - PC Scribe');
    $mailer->setBody($body);

    //send email
    $send =& $mailer->Send();
    if ($send !== true)
    {
        //error: DO NOTHING!!!
        /****DEBUGGING****/
        //$replaceText.= ' * Email error *';
        /****  CODE   ****/
    }
    else
    {
        //success: DO NOTHING!!!
        /****DEBUGGING****/
        //$replaceText.= ' (Email sent)';
        /****  CODE   ****/
    }
}

I really don't think there is anything wrong with the code (well, apart from the security concerns of using $_POST).

At least the code itself does it job.

$user_info = array();
$user = JFactory::getUser();
if ($user->guest == true)
{
    $user_info['name'] = 'A guest';
    $user_info['username'] = 'Guest';
}
        else
{
    $user_info['name'] = $user->name;
    $user_info['username'] = $user->username;
}

var_dump($user_info);

The only explication maybe that the extension works somehow outside the Joomla! and the user session is somehow lost.

Do some tests yourself, checking the contents with var_dump($user) of $user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM