How to include JFactory in separate PHP code?!

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
mhamberg
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Jun 28, 2006 10:43 pm

How to include JFactory in separate PHP code?!

Post by mhamberg » Sun Aug 10, 2008 8:58 pm

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

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

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

Post by dam-man » Sun Aug 10, 2008 9:01 pm

Mod Note: Topic moved to Joomla! 1.5 Development Forum
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

whurley
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 103
Joined: Thu Aug 24, 2006 3:59 pm

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

Post by whurley » Mon Aug 11, 2008 7:51 pm

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.

mhamberg
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Jun 28, 2006 10:43 pm

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

Post by mhamberg » Tue Aug 12, 2008 11:40 pm

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');

cyberpasta
Joomla! Intern
Joomla! Intern
Posts: 81
Joined: Fri Feb 20, 2009 12:30 am

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

Post by cyberpasta » Mon May 11, 2009 12:23 am

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??

User avatar
autodafe
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Sun Apr 06, 2008 11:29 pm
Location: Italy
Contact:

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

Post by autodafe » Wed May 20, 2009 10:54 am

your code gives me a :

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

any idea?? what's missing?

cyberpasta
Joomla! Intern
Joomla! Intern
Posts: 81
Joined: Fri Feb 20, 2009 12:30 am

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

Post by cyberpasta » Wed May 20, 2009 12:22 pm

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');
}

User avatar
autodafe
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Sun Apr 06, 2008 11:29 pm
Location: Italy
Contact:

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

Post by autodafe » Wed May 20, 2009 1:24 pm

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

unknowncat
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Aug 11, 2011 8:20 am

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

Post by unknowncat » Thu Sep 08, 2011 11:54 pm

so thankful for your work. solved my issue i just tried a couple hours on. Thank You!
Barter Exchange Management (http://www.barter-sites.com) for Joomla!


Locked

Return to “Joomla! 1.5 Coding”