0

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?

  • Do you only have 1 Joomla site hosted on your new server? – Lodder Apr 25 '16 at 16:29
  • Yes... But since it is being called directly from a browser and pointing specifically to the directory of Joomla shouldn't it not make a difference? I ask because I have another site that will have multiple Joomla installations that will use this code as well. – Charles Wilkins IV Apr 25 '16 at 16:31
  • Ok, where is your script located in regards to the Root of your Joomla installation? For example JOOMLA_ROOT/folder/myscript.php – Lodder Apr 25 '16 at 16:33
  • public_html/MyApp/_includes/php/GetJoomUser.php and Joomla_Root is public_html/site.com/cms – Charles Wilkins IV Apr 25 '16 at 16:37
  • I just tested while looking at the database table _session. The Guest User is not present until I call this file. Upon call, it is creating the guest login record in _session – Charles Wilkins IV Apr 25 '16 at 16:38
1

So I've tried replicating your directory path and I believe you may have set the paths up incorrectly.

The following worked for me:

<?php

// Constants
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/../../../site.com/cms'));
define('DS', DIRECTORY_SEPARATOR);

// Require
require_once JPATH_BASE .DS.'includes'.DS.'defines.php';
require_once JPATH_BASE .DS.'includes'.DS.'framework.php';

// Application
JFactory::getApplication('site')->initialise();

// Funtions
function GetJoomUser()
{
    $user = JFactory::getUser();

    $JomUser['id']         = $user->id;
    $JomUser['usergroups'] = $user->groups; 
    $JomUser['username']   = $user->username;

    return $JomUser;
}

function getJoomlaUsername($id)
{
    $user = JFactory::getUser($id);

    return $user->get('username');
}

// Call functions
$GetJoomUser       = GetJoomUser();
$getJoomlaUsername = getJoomlaUsername($GetJoomUser['id']);

// Dump Data
var_dump($GetJoomUser);
var_dump($getJoomlaUsername);

Try replacing all the code in your PHP file with the above code.

It returned the following for me:

array (size=3)
  'id' => string '244' (length=3)
  'usergroups' => 
    array (size=1)
      8 => string '8' (length=1)
  'username' => string 'Lodder' (length=9)

string 'Lodder' (length=9)

On a side note, your GetJoomUser is pointless. There's no need to create a new array of object values when you can simply call each one like so:

$user->id;
$user->groups; 
$user->username;
  • Actually, I think I found the root cause: Upon initial login, it creates a cookie in Chrome for p3live.com.p3proswing.com (linked to user p3.chuck [my username]). Once the script is run, it creates a second cookie for www.p3live.com (which is linked to a Guest session). So, it would appear it is then reading the p3live.com cookie to get the current user. I imagine it is creating this cookie because it does not find one for www.p3live.com. Now I just need to know who to set it so the cookie is correct – Charles Wilkins IV Apr 27 '16 at 16:44
  • JFactory::getUser() get's the user object based on database data, not cookies...I believe – Lodder Apr 27 '16 at 17:28
0

Ok... I solved the problem. It was all based on the new structure and the cookies.

I had to set System->Global Configuration->Cookie Domain to '.p3proswing.com' (as this is were the pages that reference this stored (stored in p3proswing.com/P3Live).

Next I needed to be sure all my menu items are pointed to www.p3proswing.com/P3Live/.... urls.

Then it writes the cookie to www.p3proswing.com and reads from it when the pages call the GetUserInfo3.php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.