February 24, 2012

Two (almost) identical pieces of code produce separate results

Question by Marc Towler

I have been working on a little MVC project to assist in my self-learning and I have come across an issue that completely baffled me. I made a blog section in this MVC-ish system and pulled user permissions from an ACL with no problem whatsoever.
I moved onto creating a member section and as soon as i added any permissions checking I get the following error from Chrome:

No data received
Unable to load the web page because the server sent no data.
Here are some suggestions:
Reload this web page later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

I thought it was weird, so I double checked my error logs and nothing had shown up. So I decided to copy and paste the working blog code into the member file, reloaded and i got the EXACT same error, the only difference between the two files right now is the file name and the class name.
Here is the Blog code:

<?php
class blog extends frontController {
    public $model;
    public $user;

    public function __construct()
    {
        parent::__construct();

        $this->model = $this->autoload_model();
        $this->user  = $this->load_user();
        $this->user->getUserRoles();
    }

    public function index()
    {
        //Will only list the latest post ;)
        if(!$this->user->hasPermission('blog_access'))
        {
            $array = $this->model->list_posts();

            if(empty($array))
            {
                $this->variables(array(
                    'site_title' => 'View Blog Posts',
                    'post_title' => 'Sorry but there are no posts to display'
                ));
            } else {
                $this->variables(array(
                    'site_title' => 'View Blog Posts',
                    'list'       => $array[0],
                    'post_title' => $array[0]['entry_title'],
                    'link'       => str_replace(' ', '_',$array[0]['entry_title']),
                ));
            }
        } else {
            $this->variables(array(
                'site_title' => 'Error :: Design Develop Realize',
                'body'       => 'Sorry, but you do not have permission to access this',
            ));
        }

        $this->parse('blog/list', $this->toParse);
    }

This is the member file:

<?php

class member extends frontController {
    public $model;
    public $user;

    public function __construct()
    {
        parent::__construct();

        $this->model = $this->autoload_model();
        $this->user  = $this->load_user();
        $this->user->getUserRoles();
    }

    public function index()
    {
        //Will only list the latest post ;)
        if(!$this->user->hasPermission('blog_access'))
        {
            //$array = $this->model->list_posts();

            if(empty($array))
            {
                $this->variables(array(
                    'site_title' => 'Design Develop Realize :: View Blog Posts',
                    'post_title' => 'Sorry but there are no posts to display'
                ));
            } else {
                $this->variables(array(
                    'site_title' => 'Design Develop Realize :: View Blog Posts',
                    'list'       => $array[0],
                    'post_title' => $array[0]['entry_title'],
                    'link'       => str_replace(' ', '_',$array[0]['entry_title']),
                ));
            }
        } else {
            $this->variables(array(
                'site_title' => 'Error :: Design Develop Realize',
                'body'       => 'Sorry, but you do not have permission to access this',
            ));
        }

        $this->parse('blog/list', $this->toParse);
    }

In the member class, if I comment out $this->user = $this->load_user(); then the error disappears!!!
Just for reference here is that function:

protected function load_user()
{
    if(!$this->loader->loaded['acl'])
    {
        $this->loader->loadCore('acl');
    }

    return $this->loader->loaded['acl'];
}

Any help or suggestions would be appreciated as I am stumped!

PS yes I do have error reporting set to cover everything and no it does not log anything!

EDIT: Because all files go through index.php I have placed the error reporting there:

<?php
error_reporting(E_ALL);
ini_set('date.timezone', "Europe/London");

require_once('system/library/loader.php');

$loader = new loader();
$loader->loadCore(array('frontController', 'routing'));

EDIT 2: loadCore() is below

public function loadCore($toLoad, $params = false)
{
    //important task first, check if it is more then 1 or not
    if(is_array($toLoad))
    {
        //more then one so lets go to the task!
        foreach($toLoad as $file)
        {
            if(file_exists('system/library/' . $file . '.php'))
            {
                require_once('system/library/' . $file . '.php');

                if($params)
                {
                    $this->loaded[$file] = new $file($params);
                } else {
                    $this->loaded[$file] = new $file;
                }
            } else {
                trigger_error("Core File $file does not exist");
            }
        }
    } else {
        //Phew, less work, it is only one!
        if(file_exists('system/library/' . $toLoad . '.php'))
        {
            require_once('system/library/' . $toLoad . '.php');

            if($params)
            {
                echo(__LINE__); exit;
                $this->loaded[$toLoad] = new $toLoad($params);
            } else {
                $this->loaded[$toLoad] = new $toLoad;
            }
        }
    }
}

Update: I modified loadCore so that if it was the acl being called it would use a try…catch() and that has not helped as it will not display an error just the same chrome and IE pages

Update 2: I have spoken with my host and it seems that everytime this error occurs, apache logs the following (not sure why I cannot see it in my copy of the logs!)

[Wed Feb 22 08:07:11 2012] [error] [client 93.97.245.13] Premature end
of script headers: index.php

Answer by Starx

“Premature end of script headers” are internal server errors. Which generally occurs when script breaks and does not send any HTTP headers before send the error messages. There might be several causes to this.

  • One might be output buffering. May be the server you are using buffers the output by default. I will suggest turning off the output_buffering using output_buffering = off on php.ini [docs here].

  • Make sure you are sending correct HTTP headers also

    print “Content-type: text/htmlnn”;

There are few more suggestion on this link.
To learn more about this error, go here.

Hope it helps

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!