Small strange problem

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
GacekSSJ4
Forum Newbie
Posts: 2
Joined: Sun Jan 03, 2010 7:58 pm

Small strange problem

Post by GacekSSJ4 »

Hello everyone, nice to meet you, it's my first topic on this forum.

My problem is. I have 2 codes.
Code one

Code: Select all

        while ($mysql_query_array_x = mysql_fetch_row($mysql_query_id)){
            echo '<li><a class="menu" href="index.php?page=article&aid='.$mysql_query_array_x[0].'" title="'.$mysql_query_array_x[2].'">'.$mysql_query_array_x[1].'</a></li>';
        }
Code two

Code: Select all

            setcookie('username', $login, time()-600);
            setcookie('pass', $pass, time()-600);
            echo '<div style="text-align: center; margin: 90px 0;"><h3>Wylogowywanie...</h3><img src="images/bar_loading.gif"><br />';
            echo 'Za chwile zostaniesz przeniesiony na stron? g?ówn?.</div>';
            header('Refresh: 1; url=index.php');
Strange thing is. If code one is executed like in 1st example. Code 2 does not: setcoockie username and pass nor allows header() to work.
Everything works just fine when i put one lime comment in code one. Just like:

Code: Select all

        while ($mysql_query_array_x = mysql_fetch_row($mysql_query_id)){
            //echo '<li><a class="menu" href="index.php?page=article&aid='.$mysql_query_array_x[0].'" title="'.$mysql_query_array_x[2].'">'.$mysql_query_array_x[1].'</a></li>';
        }
So putting

Code: Select all

echo '<li><a class="menu" href="index.php?page=article&aid='.$mysql_query_array_x[0].'"
into comment, allows code two execute.

What code one generates and outputs to page is:

Code: Select all

<li><a class="menu" href="index.php?page=article&aid=11" title="Komunikacja Miejska">Komunikacja</a></li><li><a class="menu" href="index.php?page=article&aid=10" title="Informacje Turystyczne">Turystyka</a></li><li><a class="menu" href="index.php?page=article&aid=1" title="Konstancin jako Uzdrowisko">Zdrowie</a></li>
Cut from Firefox. Same thing happens on IE.

I got completly no idea what to do....
PS: Code one is a method of a class and is called first
Code two is simple function called further in script.
Server have output buffering enabled.
edit:
Ok. i changed "output_buffering = 5120" from "output_buffering = 4096" and it started working... does it mean, my page can't fit into buffer and ouputs something before outputs headers (incl cookies)??
When i Removed one link (set it not to display) cookies executed but header() did not. Does it mean my buffor is too small?? What size would you recommend to set it too??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Small strange problem

Post by requinix »

It means you were trying to do something with headers, which require no output sent, after outputting something.

I strongly advise you try to rearrange your "codes" so that the setcookies and headers come before any echos or prints. It's good design and less likely to break when unexpected things happen.
GacekSSJ4
Forum Newbie
Posts: 2
Joined: Sun Jan 03, 2010 7:58 pm

Re: Small strange problem

Post by GacekSSJ4 »

That's why I always had output_buffering enabled, so i could send headers from any part of my script.
Ichecked weight of html code sent till that part of script. with function enabled it's 4418 bytes... so above my buffer size. after those 3 lines got removed, code had 4083 bytes, so was fiting buffer size :). Shame I didn't know data were sent when buffer was full (it's logical tho)
im gonna change my buffor to 8KB :) Just to be sure :)
Post Reply