Page 1 of 1

How to include JFactory in separate PHP code?!

Posted: Sun Aug 10, 2008 8:58 pm
by mhamberg
I wrote an application that I want to interface with Joomla. Everything works fine, but the final piece is that when a member views the page, I want them to see only their own information. This means that I have to know which user is viewing the page.

I'm trying to use this code to do this:

Code: Select all

require('../libraries/joomla/factory.php');
$user =& JFactory::getUser();
$user_id = $user->id;
Once I know the user id I can do my own processing. However, the result of this code is:

Fatal error: Call to undefined function jimport() in /home/(username)/public_html/libraries/joomla/factory.php on line 159

How do I make it so that I can grab the userid of the viewing user in my application? Please help!

Thank you

Re: How to include JFactory in separate PHP code?!

Posted: Sun Aug 10, 2008 9:01 pm
by dam-man
Mod Note: Topic moved to Joomla! 1.5 Development Forum

Re: How to include JFactory in separate PHP code?!

Posted: Mon Aug 11, 2008 7:51 pm
by whurley
mhamberg wrote:I wrote an application that I want to interface with Joomla. Everything works fine, but the final piece is that when a member views the page, I want them to see only their own information. This means that I have to know which user is viewing the page.

I'm trying to use this code to do this:

Code: Select all

require('../libraries/joomla/factory.php');
$user =& JFactory::getUser();
$user_id = $user->id;
Once I know the user id I can do my own processing. However, the result of this code is:

Fatal error: Call to undefined function jimport() in /home/(username)/public_html/libraries/joomla/factory.php on line 159

How do I make it so that I can grab the userid of the viewing user in my application? Please help!

Thank you
If you look in index.php you'll see the following

Code: Select all

define('JPATH_BASE', dirname(__FILE__) );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
Those two includes import the framework, including JFactory, into the execution path. Be aware that you may run into other issues related to including the Joomla! framework in your application. Several are technical, such as how Joomla! handles sessions, the possibility of function/class collision, etc. Another one is legal in nature. Joomla! is licensed under the GNU Public License (GPL) and the license requires any derived work that is distributed to be licensed under a compatible license.

Re: How to include JFactory in separate PHP code?! [SOLVED]

Posted: Tue Aug 12, 2008 11:40 pm
by mhamberg
Thanks, I had to fix a couple of other things like you mentioned. I ended up having to move the code into the root directory because putting it in a subdirectory was giving me 'restricted access.' I also had to initialize the application.

Here's the code that I needed in total:

Code: Select all

// Set flag that this is a parent file
define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(__FILE__) );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

require('libraries/joomla/factory.php');

// initialize the application 
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

....

// Take in user id parameter to only show for a specific user
$user =& JFactory::getUser();
$user_id = $user->get('id');

Re: How to include JFactory in separate PHP code?! [SOLVED]

Posted: Mon May 11, 2009 12:23 am
by cyberpasta
mhamberg wrote:Thanks, I had to fix a couple of other things like you mentioned. I ended up having to move the code into the root directory because putting it in a subdirectory was giving me 'restricted access.' I also had to initialize the application.

Here's the code that I needed in total:

Code: Select all

// Set flag that this is a parent file
define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(__FILE__) );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

require('libraries/joomla/factory.php');

// initialize the application 
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

....

// Take in user id parameter to only show for a specific user
$user =& JFactory::getUser();
$user_id = $user->get('id');
Hi!!

I've been using this same code to integrate tinybrowser with tinyMCE editor with user folders function. The folders are created correctly by userid, the upload procees is working fine....but finally the click to insert function from tinybrowser is broken, if I deleted the code to get joomla userid, the "click to insert" works again.

Any idea how can I solve this?? Maybe deinit framework after I get userid?? If so, how can I deinit framework??

Re: How to include JFactory in separate PHP code?!

Posted: Wed May 20, 2009 10:54 am
by autodafe
your code gives me a :

"No configuration file found and no installation code available. Exiting.."

any idea?? what's missing?

Re: How to include JFactory in separate PHP code?!

Posted: Wed May 20, 2009 12:22 pm
by cyberpasta
autodafe wrote:your code gives me a :

"No configuration file found and no installation code available. Exiting.."

any idea?? what's missing?
Try the next, it's working very nice for me: (notice that you must change the number for $currentfolderlevel

Code: Select all

define( 'DS', DIRECTORY_SEPARATOR );
$rootFolder = explode(DS,dirname(__FILE__));
	
//current level in diretoty structure
$currentfolderlevel = 9;

array_splice($rootFolder,-$currentfolderlevel);

$base_folder = implode(DS,$rootFolder);


if(is_dir($base_folder.DS.'libraries'.DS.'joomla'))   
{
	
	define( '_JEXEC', 1 );
	
	define('JPATH_BASE',implode(DS,$rootFolder));
	
	require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
        require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
        $userid='';
        $usertype='';
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user =& JFactory::getUser();
$userid = $user->get('id');
$usertype = $user->get('usertype');
}

Re: How to include JFactory in separate PHP code?!

Posted: Wed May 20, 2009 1:24 pm
by autodafe
I solved it by putting my php file inthe root directory...Which looks more ore less the direction you ponted me to with your code...
ut then I realized that this was not what I needed and had to wor out something else.
Thanks a lot anyway

Re: How to include JFactory in separate PHP code?!

Posted: Thu Sep 08, 2011 11:54 pm
by unknowncat
so thankful for your work. solved my issue i just tried a couple hours on. Thank You!