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.
help with getUser
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
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
- jackrabbit
- Joomla! Ace
- Posts: 1473
- Joined: Thu May 21, 2009 3:12 am
- Location: Florida
- Contact:
Re: help with getUser
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.
When connecting to the DB you should use the getDbo() object. See http://docs.joomla.org/Selecting_data_using_JDatabase
Code: Select all
<?php
defined('_JEXEC') or die('Restricted access');
$user = JFactory::getUser();
echo $user->username;
Reset Joomla super user password and username simply | http://cmsenergizer.com/website-energy- ... d-remotely
- toivo
- Joomla! Master
- Posts: 17439
- Joined: Thu Feb 15, 2007 5:48 am
- Location: Sydney, Australia
Re: help with getUser
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
-
- Joomla! Apprentice
- Posts: 9
- Joined: Sat May 24, 2008 7:05 pm
Re: help with getUser
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.
When connecting to the DB you should use the getDbo() object. See http://docs.joomla.org/Selecting_data_using_JDatabaseCode: Select all
<?php defined('_JEXEC') or die('Restricted access'); $user = JFactory::getUser(); echo $user->username;
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.
- jackrabbit
- Joomla! Ace
- Posts: 1473
- Joined: Thu May 21, 2009 3:12 am
- Location: Florida
- Contact:
Re: help with getUser
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
- toivo
- Joomla! Master
- Posts: 17439
- Joined: Thu Feb 15, 2007 5:48 am
- Location: Sydney, Australia
Re: help with getUser
It cannot if this script is the entry point, instead of index.php in the main folder.jackrabbit wrote:This line must be declared before any thingCode: Select all
defined('_JEXEC') or die('Restricted access');
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