I'm a newbie but trying to learn.
I have a script to read the joomla user details. It's a simple script just to display logged in user name. The script works if I put it in the root directory of my website.
What I want to do is run the script from a folder off the root, /ssscart, for example.
When I put the script in the sub-directory it does not execute.
Here is my script:
<?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;
echo 'hello';
echo " - ".$joomla_username;
?>
I believe my problem is in the JPATH_BASE entry but haven't been able to figure it out yet.
Thanks
G
Try this,
The is issue is due to JPATH_BASE path getting wrongly.
when you are in root it will look like.
define('JPATH_BASE', dirname(__FILE__) );
So in your case you are in sub folder so it will be look like.
define('JPATH_BASE', dirname(dirname(__FILE__)) );
so your final code will be look like.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(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();
Hope it works..
Related
So I wrote up this code to display the cart total outside of joomla framework:
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
$total += $product[quantity];
}
echo $total;
?>
which works fine (by displaying the total items in cart) in a top level directory (/public_html/test.php)
but if I move it to a second-level directory, like (/public_html/includes/test.php), I get this error:
first, the code (notice the /../store because we're in a second-level now):
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
$total += $product[quantity];
}
echo $total;
?>
then the error:
Fatal error: Uncaught exception 'Exception' with message 'XML file did not load' in /home/me/public_html/store/libraries/joomla/form/form.php:2020 Stack trace: #0 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmplugin.php(201): JForm::getInstance('weight_countrie...', '/home/me/publ...', Array, false, '//vmconfig | //...') #1 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmpsplugin.php(45): vmPlugin::getVarsToPushByXML('/home/me/publ...', 'weight_countrie...') #2 /home/me/public_html/store/plugins/vmshipment/weight_countries/weight_countries.php(44): vmPSPlugin->getVarsToPush() #3 /home/me/public_html/store/libraries/joomla/plugin/helper.php(194): plgVmShipmentWeight_countries->__construct(Object(JDispatcher), Array) #4 /home/me/public_html/store/libraries/joomla/plugin/helper.php(125): JPluginHelper::_import(Object(stdClass), true, NULL) #5 /home/me/public_html/store/administrator/components/com_virtuemart/helpe in /home/me/public_html/store/libraries/joomla/form/form.php on line 2020
I have no clue why it works fine in the top level but then not in subdirectories. Any ideas?
When you are going to subdirectory the path changes. I got these paths for the defined JPATH_BASE.
For top level
string '/Applications/MAMP/htdocs/store';
And for subdirectory
string '/Applications/MAMP/htdocs/test/../store';
As dirname(realpath(FILE) will give the location of current directory it is in.
So there are two methods to get correct path
JPATH_BASE can be given the exact location like
/var/www/store for linux system
/Applications/MAMP/htdocs/store for MAC
C:/xampp/htdocs/www/store for windows
example
define('JPATH_BASE', '/var/www/store' );
These are just examples.
It can also be achieved this way by defining JPATH_BASE this way
Replace
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
BY
$path=getcwd();
$parts = explode(DIRECTORY_SEPARATOR, $path);
$pop = array_pop($parts);
$path = implode(DIRECTORY_SEPARATOR, $parts);
define('JPATH_BASE', $path.'/store');
I have created an external file. I want to load joomla default template in that file. I used below code, and it caused an error:
Fatal error: Call to protected method JApplicationSite::render() from
context ' '
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', realpath(dirname(__FILE__) ));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');
JPluginHelper::importPlugin('system');
$mainframe->initialise();
$myContent = "Hello World!!";
$document = JFactory::getDocument();
$document->setBuffer($myContent, 'component');
$mainframe->render();
echo $mainframe;
At the least you need to let the Joomla app know which template you'd like to render.
//normal external script initialisation
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath('/home/somepath/public_html/somedirectory/'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
require_once( JPATH_LIBRARIES .DS.'joomla'.DS.'factory.php');
$app = JFactory::getApplication('site');
$app->initialise();
//jimport( 'joomla.document.html.html' ); required in certain cases
//initialise the doc to use the Html renderer
$doc = new JDocumentHtml();
//minimal info required
$options['directory'] = "/my/template/directory";
$options["template"] = $app->getTemplate();//get the template name
$options["file"] = "index.php";//usually
//Optional: Manually set params
//$params = new JRegistry();
//$params->colorVariation = "blue";
//$params->widthStyle = "fluid";
//$params->showComponent = "1";
//$options["params"] = $params;
//you may initialise the document object here and set the buffers
//$document = JFactory::getDocument();
//$document->setBuffer($contents, 'module', $name);
// $document->setHeadData($data['head']);etc
echo $doc->render(false, $options);
$app->close();
Of course dont expect it to display the same way esp if you use relative urls. Can be used for other purposes though like examining the scripts in a document template. tested on Joomla 3.3 PHP 5.5 . It will not work on lower versions like 1.5.
I am trying to load a module on an external page from the Joomla core.
I am using the following:
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' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('xxxx', 'xxxxxxx');
$memberships = JModuleHelper::renderModule($module);
But get the following error
JPath::check Use of relative paths not permitted
Any ideas
define( '_JEXEC', 1 );
define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT']);
$mosConfig_absolute_path = 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_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'methods.php');
require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'base'.DS.'object.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'database'.DS.'database.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'database'.DS.'database'.DS.'mysql.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'folder.php');
It worked when the .php file was in the root folder, but when I call it from subdomain I had an error:
XMLHttpRequest cannot load the php <my php file>. Origin <subdomain> is not allowed by Access-Control-Allow-Origin.
Frankly speaking it works(sends me an e-mail), but callback doesn't...
So I moved the php file to subdomain directory. Now I have 500 error. Need help. Thank you!
All Domains:
<?php
header('Access-Control-Allow-Origin: *');
?>
Allowed Domains:
<?php
header('Access-Control-Allow-Origin: http://domain1.com, http://domain2.com');
?>
i have this code which will check whether (a) the store is online and (b) the directory /store is available:
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe =& JFactory::getApplication('site');
$filename = '../store/';
if (!$mainframe->getCfg('offline') && file_exists($filename))
{
echo "online";
}
else
{
echo "offline";
}
?>
when the i set the store as offline in my control panel it works fine, however when the directory /store has it's name changed or is deleted (becomes unavailable) the page generates a server error whereas it should echo "offline". how can i modify it so that when the directory names changes, it changes it echoes "offline"
You must check the directory prior to the initialization of the application, since the application relies on the directory's existence.
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
if (!file_exists(JPATH_BASE)) {
echo 'Offline due to missing installation';
exit(0);
}
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$app = JFactory::getApplication('site');
if ($app->getCfg('offline')) {
echo 'Offline due to configuration setting';
$app->close();
}
echo 'Online';