Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,688 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,274 people online right now. Registration is fast and FREE... Join Now!




error, cant figure out

 
Reply to this topicStart new topic

error, cant figure out

musya
post 31 May, 2008 - 02:54 PM
Post #1


D.I.C Regular

Group Icon
Joined: 25 Apr, 2007
Posts: 291



Thanked 1 times

Dream Kudos: 50
My Contributions


I get errors on these lines of code in my script, and I can't seem to figure it out. Does anybody know whats going on?

Here is the code
php

setcookie("user", $result['id'], time()+3600);
header( 'Location: client/index.php' ) ;


Here is the error
CODE

Warning: Cannot modify header information - headers already sent by (output started at /home/ils/public_html/protocal/login.php:12) in /home/ils/public_html/protocal/login.php on line 14
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 04:13 PM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


You'll get that error when you send something to be printed to the browser before you set the header. You need to do your header before you output any data. If you have something like this

CODE

<?php
    // stuff

   echo "<table><tr><td>Hi</td></tr></table>";

   setcookie("user", $result['id'], time()+3600);
   header('Location: client/index.php');
?>


You'll get a header already sent error. You need to put the header redirect before any echo statements, or any html that you have in the code. You may have to rethink the setup of that file or process. If you post the whole thing we'll be able to help you find the specific area where the problem started. Otherwise you can figure it out by running through your code.
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 31 May, 2008 - 04:29 PM
Post #3


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


actually akozlik, you need to echo the header....

just make it:
php
echo "header('Location: client/index.php')";

instead of
php
header('Location: client/index.php');
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 May, 2008 - 04:36 PM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Actually JBrace, akozlik is right. Header is a function and is not something you echo such as a statement or variable. If a user sends data to the client, the server automatically flushes the headers and sends those. Thus if you try to use a header call (which modifies headers) it will error because the headers have already been given to the client in the response stream.

header() appends header information to the headers of a response page. Any output sent to the client (a space, a tab, some info that has been echoed or simply printed) causes headers to be sent and, if prior to a header() call, it will cause the header() call to fail.

Read up on the user of header() at php.net to learn more. smile.gif

This post has been edited by Martyr2: 31 May, 2008 - 04:37 PM
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 31 May, 2008 - 04:43 PM
Post #5


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


QUOTE(Martyr2 @ 31 May, 2008 - 05:36 PM) *

Actually JBrace, akozlik is right. Header is a function and is not something you echo such as a statement or variable. If a user sends data to the client, the server automatically flushes the headers and sends those. Thus if you try to use a header call (which modifies headers) it will error because the headers have already been given to the client in the response stream.

header() appends header information to the headers of a response page. Any output sent to the client (a space, a tab, some info that has been echoed or simply printed) causes headers to be sent and, if prior to a header() call, it will cause the header() call to fail.

Read up on the user of header() at php.net to learn more. smile.gif

*dies* i'm not thinking correctly today.. =/ been spending too much time in the sun T.T
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 May, 2008 - 04:45 PM
Post #6


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


I think we all have been enjoying the sun much these last couple of weeks. Another beautiful sunny day here in Vancouver Canada. cool.gif icon_up.gif

This post has been edited by Martyr2: 31 May, 2008 - 04:46 PM
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 31 May, 2008 - 04:49 PM
Post #7


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


not enjoying it here.... i'm stuck doing yard work.... my skins like wicked dark now, and i'm usually really light yinyang.gif

i'd swear to god, i look like a miner D=
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 04:52 PM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(JBrace1990 @ 31 May, 2008 - 05:49 PM) *

not enjoying it here.... i'm stuck doing yard work.... my skins like wicked dark now, and i'm usually really light yinyang.gif

i'd swear to god, i look like a miner D=


Blah, yardwork sucks. That's the nice thing about living in an apartment I guess. It's been hot as balls in Florida. I don't even want to think about going outside during the day.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 31 May, 2008 - 06:28 PM
Post #9


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


I get easily irritated when working with cookies in PHP, & the code isn't just so, & I get that header error.
User is offlineProfile CardPM

Go to the top of the page

chrisman
post 1 Jun, 2008 - 11:57 AM
Post #10


New D.I.C Head

Group Icon
Joined: 22 Mar, 2008
Posts: 41



Thanked 1 times

Dream Kudos: 100
My Contributions


I always forget that headers have to be before ANYTHING, including my <!DOCTYPE> and <html> tags.
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 1 Jun, 2008 - 01:27 PM
Post #11


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


^ The trickiest thing about scripts, and probably the best advice on things like this is, any whitespace such as this
CODE

<?php
//do something
?>



Or this
CODE


<?php
//stuff


Any whitespace before, after, in between php tags will cause output and mess with your headers and such.

Best advice is whenever you include files, on all files included, its best practice to leave off the trailing ?> so that you do not have erroneous output.

This post has been edited by joeyadms: 1 Jun, 2008 - 01:29 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:23AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month