3

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?

3 Answers 3

3

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 .

1
  • 1
    just to point out that a whitespace before <?php was the issue in my case
    – cmario
    Mar 29, 2017 at 16:42
1

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.

1

Kindly follow the below steps this will probably solve the issue.

  1. Enter your Joomla site's backend
  2. Select from the admin menu: Extensions -> Manage -> Database
  3. Click on the FIX button. You may find that some other updates need to be done to bring the database up to 3.5.
  4. Head to Global Configuration, and turn the site 'off' before this next step to discourage any users for a while.
  5. Then, as mentioned above, head to phpMyAdmin and delete all the entries in the xxxxx_sessions table.
  6. Head to the backend and turn the site back on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.