Connecting to Joomla 1.5 from an external PHP script

Monday, 01 June 2009

Sometimes you need to access Joomla libraries and/or data from an outside script. You may need to sync users with a 3PD application, or maybe you're developing a fresh application, but you're used to Joomla style  JTable database manipulation, and JRequest input filtering.

Here's the code to boot Joomla from an external script. Back when J! version was 1.5.7, this script was tested and working with PHP CLI(command line), too. I did not test with 1.5.10 for command line compatibility.

 Of course, the JDocument instance is not available, because we do not render anything, we're just initializing the application.

The code sample below includes a simple login check. this login check will work as long as the connector script is hosted under the same directory as Joomla, because it will check cookies using HTTP path from the request URI.

<?php 
 
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
// JPATH_BASE should point to Joomla root directory
// if you app is placed into a subfolder in Joomla root, the path will look like dirname(__FILE__) . '/..'
define( 'JPATH_BASE', realpath(dirname(__FILE__) .'/joomla1510' ) );
define( 'DS', DIRECTORY_SEPARATOR );
 
//as seen on
http://forum.joomla.org/viewtopic.php?f=304&t=263491
 
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
 
 
 
/**
 * check_joomla_login
 *
 * @return BOOL
 * @author Tudor
 *
 * USAGE EXAMPLE
 * echo (check_joomla_login())?"Logged in" : "Not logged in";
 **/
 
 
function check_joomla_login(){
 
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// print_r($mainframe);
$user
=& JFactory::getUser();
// $session
=& JFactory::getSession();
// debug print
// print_r($user);
// print_r($session);
return ( (int)$user->id==0 )? FALSE:TRUE;
}

 

Are you a Joomla/PHP/Web developer? You may want to check our list of freelance jobs websites.



Give us some social love (it really works now)!

Reddit! Del.icio.us! StumbleUpon! Yahoo! Swik!



Be first to comment this article
RSS comments

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Please don't use comments to plug your web site. Links are rel='nofollow'-ed
  • Please refresh the page if you're having trouble with the security image code
Name:
E-mail
Homepage
Title:
Comment:

:) :grin ;) 8) :p
:roll :eek :upset :zzz :sigh
:? :cry :( :x
Code:* Code

Last Updated ( Thursday, 18 June 2009 )
 
Post and Bid on Joomla/PHP projects! Join EUFreelance.com for free!

Europe freelancer directory

Newsletter

Subscribe to TeachMeJoomla's newsletter
Name:
Email:


Auto tags

joomla external login

joomla login script

joomla php script

joomla login php

joomla external database

joomla external php

joomla insert php code

external login joomla

joomla mainframe

php in joomla article

joomla $mainframe

joomla login check

joomla JPATH_BASE

joomla check login

joomla php scripts

php script joomla

joomla external script

joomla php

joomla 1.5 Login check

joomla login from external page

joomla script

joomla user login check