I've tried moving my site and my script to get the Joomla User Information is returning 0 (the guest account) despite being logged in.
The only change I had to make, once moving servers, was the location of the $JRoot folder to match the new server layout.
<?php
$temp = $_SERVER['DOCUMENT_ROOT'];
$temp = str_replace("one_url", "another_url", $temp);
if($_SERVER['SERVER_ADDR'] =='::1') {
$JRoot = $temp;
} else {
$JRoot = $temp;
}
define( '_JEXEC', 1 );
define('JPATH_BASE', $JRoot );
define('JPATH_ROOT', $JRoot );
define( 'DS', DIRECTORY_SEPARATOR );
$defines = $JRoot . "/includes/defines.php";
$framework = $JRoot . "/includes/framework.php";
echo "defines: " . $defines . "<br />";
echo "DS:" . DS . "<br />";
require_once ($defines);
require_once ($framework);
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
function GetJoomUser() {
$user = JFactory::getUser();
$JomUser['id'] = $user->id;
$JomUser['usergroups'] = $user->groups;
$JomUser['username'] = $user->username;
print_r($user);
return $JomUser;
}
function getJoomlaUsername($id) {
$user = JFactory::getUser($id);
$username=$user->get('username');
return $username;
}
This code is working because I've used file_exists statements (in testing) to assure defines & framework are being found.
When logged in, I can see my User Profile so I know that I'm successfully logged in.
So why is $user printing out the Guest information?
JOOMLA_ROOT/folder/myscript.php