help with getUser

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Locked
Txchaser
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Sat May 24, 2008 7:05 pm

help with getUser

Post by Txchaser » Sat Aug 30, 2014 4:51 pm

Hi there,
I have been trying to get past a few issues here. I am using ajax to call a php script to get the user name of the person logged in and pass back to ajax. At 1st I was having an access control header error, which I have now gotten fixed. I now am having a problem /error when trying to get the user name from JFactory. I have tried various ways to have this work with no solution as of yet.

Here is the current code I have in my php file residing in a created folder within the joomla structure.

<?php
header('Access-Control-Allow-Origin: http://site.com');
$con=mysqli_connect("DB Host","DB User","password","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
include('/configuration.php');
$user =& JFactory::getUser();
echo $user->username;
?>

I have no DB connection issues. When running the script it shows no issues right out but has no information being sent from the DB to ajax. When pulling up the php script directly here is the error I get.

Warning: include(/configuration.php): failed to open stream: No such file or directory in /home/u293339616/public_html/test/getuid.php on line 9 Warning: include(): Failed opening '/configuration.php' for inclusion (include_path='.:/usr/lib/php') in /home/u293339616/public_html/test/getuid.php on line 9 Fatal error: Class 'JFactory' not found in /home/u293339616/public_html/test/getuid.php on line 10


Your help with this is appreciated.

User avatar
jackrabbit
Joomla! Ace
Joomla! Ace
Posts: 1473
Joined: Thu May 21, 2009 3:12 am
Location: Florida
Contact:

Re: help with getUser

Post by jackrabbit » Sat Aug 30, 2014 5:11 pm

If working within Joomla structure, you need to use the API objects. You do not need to connect to the database to get the user data when using the getUser() object.

Code: Select all

<?php 
defined('_JEXEC') or die('Restricted access');
$user = JFactory::getUser();
echo $user->username;
When connecting to the DB you should use the getDbo() object. See http://docs.joomla.org/Selecting_data_using_JDatabase
Reset Joomla super user password and username simply | http://cmsenergizer.com/website-energy- ... d-remotely

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17439
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: help with getUser

Post by toivo » Sat Aug 30, 2014 5:28 pm

If your PHP script is standalone and not a function in a component, especially if it resides in its own folder, you need to load the Joomla framework first. The following example assumes that your script is in a folder under the Joomla main folder, just one level down.

Code: Select all

<?php
// allow direct access
define('_JEXEC', 1); 	

// main-joomla-folder/myfolder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));

// bootstrap joomla 
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

$user	= JFactory::getUser();
$name = $user->name;
Toivo Talikka, Global Moderator

Txchaser
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Sat May 24, 2008 7:05 pm

Re: help with getUser

Post by Txchaser » Sat Aug 30, 2014 5:33 pm

jackrabbit wrote:If working within Joomla structure, you need to use the API objects. You do not need to connect to the database to get the user data when using the getUser() object.

Code: Select all

<?php 
defined('_JEXEC') or die('Restricted access');
$user = JFactory::getUser();
echo $user->username;
When connecting to the DB you should use the getDbo() object. See http://docs.joomla.org/Selecting_data_using_JDatabase

Hey jackrabbit,
Thanks for your fast reply I have tried your suggestion as you mentioned the php as follows and get "Restricted access"

<?php
header('Access-Control-Allow-Origin: http://site.com');
defined('_JEXEC') or die('Restricted access');
$user = JFactory::getUser();
echo $user->username;
?>

Of course I have to keep the header access control otherwise I get that error back again.
My directory structure is as follows: site.com/test/file.php so I am assuming that this is still within the joomla structure.

User avatar
jackrabbit
Joomla! Ace
Joomla! Ace
Posts: 1473
Joined: Thu May 21, 2009 3:12 am
Location: Florida
Contact:

Re: help with getUser

Post by jackrabbit » Sat Aug 30, 2014 5:50 pm

This line must be declared before any thing

Code: Select all

defined('_JEXEC') or die('Restricted access');
Reset Joomla super user password and username simply | http://cmsenergizer.com/website-energy- ... d-remotely

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17439
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: help with getUser

Post by toivo » Sat Aug 30, 2014 5:56 pm

jackrabbit wrote:This line must be declared before any thing

Code: Select all

defined('_JEXEC') or die('Restricted access');
It cannot if this script is the entry point, instead of index.php in the main folder.

Another way to create a web service for an AJAX function is to piggy back it to an existing component, in which case the defined function with die should be included.
Toivo Talikka, Global Moderator


Locked

Return to “General Questions/New to Joomla! 3.x”