I have recently upgraded to Joomla 3.5.1 and I get the following error when I load/include a php file on my root directory.
"Error displaying the error page: Application Instantiation Error: Failed to start the session because headers have already been sent by test.php at line 1"
I am loading test.php file in a module. The test.php is on the main root of joomla installation. However, since Joomla itself has already sets the session, I can't access joomla database thru test.php because of the session error above.
In the test.php, I have the following code to start with;
<?php
define( '_JEXEC', 1 );
define( '_VALID_MOS', 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();
It was working just fine before the upgrade. How can I prevent the session conflict in Joomla when I load external php file to access joomla database?
Typically this issue is caused by an almost unnoticeable space (or new line) at the beginning of the file. Check your test.php file for any spaces before the opening <?php .
I think the reason why you get this error after the 3.5.1 update is that they now throw an exception in /libraries/joomla/session/handler/native.php line 252 if header_sent() returns true. To avoid that you can try to set the "session.use_cookies" directive to false with
ini_set('session.use_cookies', 0);
But that is really more a trick than a solution you shouldn't do that. In fact I can't think of any reason why you want to reinitialize the Joomla! framework like that. The proper way is to create a file like /modules/mod_mymod/helper.php
defined('_JEXEC') or die('Restricted access');
class MyModHelper {
public static function doSth() {
$db = JFactory::getDBO();
//your code here
}
}
And then you can execute the code from almost anywhere with
require_once JPATH_ROOT.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.'mod_mymod'.DIRECTORY_SEPARATOR.'helper.php';
MyModHelper::doSth();
But I came here because I got the same error on executing a Joomla! CLI script. So just a few words to this because your heading fits somehow. On some machines the header_sent() method seems to return true even for the default Joomla! CLI scripts like "finder_indexer.php". To solve this I wrote
ob_start();
at the beginning of the script to buffer the output and prevent any CLI output before the session actually starts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With