问题
I need to get the information of the user that's currently logged in to Joomla from a program outside of Joomla itself. I upgraded from 1.5 to 2.5, and what I had before doesn't work anymore.
<?php
define( '_VALID_MOS', 1 );
include_once( 'globals.php' );
require_once( 'configuration.php' );
require_once( 'includes/joomla.php' );
$option="test";
$mainframe = new mosMainFrame( $database, $option, '.' );
$mainframe->initSession();
$my = $mainframe->getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
After some research, I've come up with this:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$my =& JFactory::getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
$joomla_username = $my->username;
It doesn't produce any errors but seems to work. However, the user object is empty. This script is in the same directory as the Joomla installation. What may be the problem? Thanks!
Sources:
http://www.cmsbloke.com/accessing-joomla-objects-from-an-external-script/
回答1:
Taken from http://docs.joomla.org/How_to_access_session_variables_set_by_an_external_script
Solution: Replace session_start();
in your external script with
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Be sure to change JPATH_BASE
to suit your directory structure.
Replace the $_SESSION[ 'name' ] = "value";
in your external script with
$session =& JFactory::getSession();
$session->set('name', "value");
Now you can retrieve this session variable using:
$session =& JFactory::getSession();
echo $session->get('name');
回答2:
It turns out, I had two problems:
- My script was on
www.example.com
, while I was logged in underexample.com
, so the different domain was throwing off the session data. - I also wasn't initialising the application, which apparently is necessary.
Here are the results:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
$joomla_name = $user->name;
$joomla_email = $user->email;
$joomla_password = $user->password;
$joomla_username = $user->username;
回答3:
I think people have had problems with this in Joomla 2.5 as things have changed. I haven't found a solution for the way you have requested but wrote this work around (excluding the password field though) that you may want to use.
define a connection to the database:
<?php
$host="localhost";
$username="DB_USERNAME";
$password="DB_PASSWORD";
$db_name="DB_NAME";
mysql_connect($host,$username,$password)or die(mysql_error());
mysql_select_db($db_name)or die(mysql_error());
?>
Echo create a query and echo the results in a table:
<table style="background:#FFF; color:#000;">
<?php
$query = "SELECT username,name,email FROM j25_users ";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<tr><td>',$row["username"],'</td><td>',$row["name"],'</td><td>',$row["email"],'</td></tr>' ;
}
?>
</table>
You will need to change j25_users to the correct prefix of your database tables.
Update: Much better to import the framework to get the user object.
Hope this comes in handy for you. Regards.
回答4:
If you want to skip using Joomla! functions, you can query the Joomla! database. Join tables js25_session
and js25_users
with the user id. This way, you can get every currently logged-in user and all guests on the website (and possibly the number of online users).
来源:https://stackoverflow.com/questions/11111541/joomla-2-5-get-user-data-from-external-script