Page 1 of 1

Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 7:15 am
by Domsore
What I am trying to acheive is to stop getting the error:

Warning: Cannot modify header information - headers already sent by (output started at -------\library\headerProperties.php:43) in --------\library\template.php on line 235

I was advised to use ob_start() and ob_flush() so I looked them up in php manual so this is what I did:

headerProperties.php:

Code: Select all

class headerProperties {

    public $title;
    public $templateName;

    public function setTemplate(){
	$result = mysql_query('SELECT * FROM sys_settings') OR die(mysql_error());
	$template = mysql_fetch_array($result);
	$this->templateName = $template['sys_site_template'];
    }

    public function selectTemplate(){
	$result = mysql_query('SELECT * FROM sys_settings') OR die(mysql_error());
	$title = mysql_fetch_array($result);
    }

    public function loadTemplate(){
	echo '<link rel="stylesheet" type="text/css" href="template'.'/'.$this->templateName.'/'.'css'.'/'.$this->templateName.'.css" />';
	echo '<link rel="stylesheet" type="text/css" href="template/component/component.css" />';
    }

    public function setTitle(){
	$result = mysql_query('SELECT * FROM sys_settings') OR die(mysql_error());
	$title = mysql_fetch_array($result);
	$this->title = $title['sys_site_title'];
    }

    public function insTitle(){
	echo '<title>'.$this->title.'</title>';
    }

    public function insMeta(){
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
    }

    public function createHeader(){
	echo '<head>';
	$this->insTitle();
	$this->insMeta();
	$this->loadTemplate();
	echo '</head>';
    }

    public function displayHeader(){
	ob_start($this->createHeader());
	ob_flush();
    }
}
template.php (not enitre code):

Code: Select all

public function logoutFunc(){

	$sub_logout = $_POST[sub_logout];
	
	if(false !== ($sub_logout == 'Logout')){
	    unset($_SESSION[user_ID]);
	    unset($_SESSION[user_name]);
	    header('refresh: 1;');
	}else{
	    echo '<form action="" method="post" name="logout">
		  <input name="sub_logout" type="submit" value="Logout" />
		  </form>';
	}
    }


public function loggedUserFunc(){
	echo 'You are logged in.';
	ob_start($this->logoutFunc());
	ob_flush();
    }
But I am still getting the error therefor I am assuming I am not using them correctly?

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 9:00 am
by anantha
you are starting the buffer and flushing at the same point...so no use of using it...

start the buffer ob_start();

then do all your code...

then do ob_get_contents and store it in a variable......since you are doing it in a function, then return those variable....then finally where ever you are outputting to the screen for the first time....after that echo the stored variable...

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 10:06 am
by Domsore
So something like:

Code: Select all


public function createHeader(){
        ob_start();

	echo '<head>';
	$this->insTitle();
	$this->insMeta();
	$this->loadTemplate();
	echo '</head>';

        $output = ob_get_contents();

        ob_end_clean();

       $this->outputvar = $output;
    }
Then on the page where I call it, call $outputvar instead of the function?

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 10:17 am
by Domsore
Please excuse the double post but these are the adjustments I made, it works but I still get the header error:

headerProperties:

Code: Select all

public function createHeader(){
	ob_start();

        echo '<head>';
        $this->insTitle();
        $this->insMeta();
        $this->loadTemplate();
        echo '</head>';

        $output = ob_get_contents();

        ob_end_clean();

	echo $output;
    }
template.php

Code: Select all

public function logoutFunc(){

	$sub_logout = $_POST[sub_logout];
	
	if(false !== ($sub_logout == 'Logout')){
	    unset($_SESSION[user_ID]);
	    unset($_SESSION[user_name]);

	    ob_start();

	    header('refresh: 1;');

	    $output = ob_get_contents();

	    ob_end_clean();

	    echo $output;

	}else{
	    echo '<form action="" method="post" name="logout">
		  <input name="sub_logout" type="submit" value="Logout" />
		  </form>';
	}

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 10:56 am
by pickle
The best way to get rid of that error is to fix the problem, not band-aid around it. Sorry for the bluntness, but the advice you got to use output buffering in the first place was just awful.

The problem you're having is that you're trying to send HTTP headers after output has been generated. PHP automatically sends appropriate HTTP headers before output is sent to the browser. So if you try to send HTTP headers after output you get that error, as you can't send HTTP headers more than once.

The issue you're having is you're trying to send the refresh header after part of the page has already been output. What exactly are you trying to accomplish? There's almost certainly a better way. Your logout function should be run very first thing - before any code that generates output.

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 11:53 am
by Domsore
Thank you for putting me straight.

I have a logout function so that when you click the logout button it unsets the session and refreshes the page... now that I have read your clear and precise post I realises that the logout function (refresh & undo session) needs to be called before the html <head> tags are set in place. I hope, that is what your saying correct?

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 11:56 am
by pickle
Yes. Do the logout before the rest of the page. The rest of the page should be doing one thing if the user is logged in, another if the user is logged out. Doing the logout right at the top of the page gives the rest of the page the chance to act accordingly.

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 12:04 pm
by Domsore
Thank you so much,

I can't believe it was as simple as this. Appologies.

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 12:07 pm
by pickle
You're welcome.

And don't apologize - everyone asks questions that seem dumb once you know the answer.

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 12:37 pm
by John Cartwright
pickle wrote:The best way to get rid of that error is to fix the problem, not band-aid around it. Sorry for the bluntness, but the advice you got to use output buffering in the first place was just awful.
I completely agree with Pickle. For some reason this advise is given far too frequently, even around here. It's just wrong.

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 6:02 pm
by Domsore
I am really struggling now,

so now that I have pointed the logout button at the actual logout page it goes to that page and happily returns them to the index.php as planned... however... the sessions do not get unset anoymore? Im having the reverse issue of what I had before. Before it would log me out but not return to index.php.

I am really not sure what's going on?

my logout button now points to a log out page which consists of:

Code: Select all

class logout {

    public function  __construct() {
	$logout = $_GET[logout];

	if(false !== ($logout == 'TRUE')){
	    unset($_SESSION['user_ID']);
	    unset($_SESSION['user_name']);

	    header('Location: ../');

	}
    }

}

$logout = new logout();
I did change it to test if the sessions were actually there to:

Code: Select all

class logout {

    public function  __construct() {
	$logout = $_GET[logout];

	if(false !== ($logout == 'TRUE')){
	    echo $_SESSION['user_ID'];
	    echo $_SESSION['user_name'];
	}
    }

}

$logout = new logout();
and it wont display the sessions (im assuming they are not set on this page)

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 6:06 pm
by pickle
It's overkill to put that in a class. Some people are anal about that sort of thing, but when you're only doing a few lines, I don't think a class is necessary.

Your if() condition is pretty funky too & may be causing the problem. Do you really need to test if $_GET['logout'] is "TRUE"? Is there ever a situation where the user would be at this page without $_GET['logout'] being "TRUE"? For that matter, is there any reason the user would be at this page without wanting to log out? If I were in your situation, I'd get rid of that if() condition & adopt the idea that if this page is loaded, the user gets logged out - period.

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 6:21 pm
by Domsore
I have removed it from a function. Also, origanally I did have it so the was no if() but for somereason it is looping? even though I havent called logout.php anywhere else?

*EDIT* Just did a find in project - logout. It was being called somewhere else... *Ditz, but it still doesnt unset the sessions

Re: Not sure if im using ob_ functions correctly

Posted: Wed Feb 09, 2011 6:34 pm
by Domsore
Finally! str_replace('Your', 'My', :google:); All I had to do was:

Code: Select all

<?php
    session_start();
    session_destroy();
    header('Location: ../');
?>
I hate simple things... I over complicate them.