Not sure if im using ob_ functions correctly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Not sure if im using ob_ functions correctly

Post 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?
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Not sure if im using ob_ functions correctly

Post 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...
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post 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?
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post 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>';
	}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Not sure if im using ob_ functions correctly

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Not sure if im using ob_ functions correctly

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post by Domsore »

Thank you so much,

I can't believe it was as simple as this. Appologies.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Not sure if im using ob_ functions correctly

Post by pickle »

You're welcome.

And don't apologize - everyone asks questions that seem dumb once you know the answer.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Not sure if im using ob_ functions correctly

Post 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.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post 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)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Not sure if im using ob_ functions correctly

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post 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
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Not sure if im using ob_ functions correctly

Post 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.
Post Reply