Welcome to WebmasterWorld Guest from 54.227.48.147

Forum Moderators: coopster & jatar k

Headers Already Sent Error

     
5:11 am on Aug 10, 2017 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts:130
votes: 2


I just got a JavaScript timeout script working for a control panel but knowing JavaScript can be turned off I figured writing a cookie with JavaScript while logging in and checking to make sure the cookie is there (re-direct them back to the login screen with a warning) would pretty much ensure that JavaScript was running. While that is one way of going about it brought on another brain buster that I can’t figure out because if I try to write the JavaScript cookie and use a location re-direct I get the following error:
Warning: Cannot modify header information - headers already sent by (output started at /blah/blah/blah/blah/not_the_actual_name.php:61) in /blah/blah/blah/blah/not_the_actual_name.php:on line 62

I’ve tried all the following and still get the error:
echo” <script>
document.cookie = ‘foo=’+’bar’;
</script>”;

I’ve switched the single quotes for double and double for singles.
I tried closing out php, ?> and writing the cookie info and returning to php, <?php
When I used to include instead of using the re-direct the script info for writing the cookie appeared on the destination page. I tried using an intermediate page for writing the cookie ( a 2nd redirect). The only thing that has worked is, I redirected to an intermediate page, put the cookie headers in the <head> section of the html followed with a meta-direct but if the page hangs up there for whatever reason all the cookie info is on the page source. I’ve done lots of searching on this one and haven’t found anything quite the same. If any info is needed I would be glad to offer. Thanx
5:16 am on Aug 10, 2017 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts:130
votes: 2


I jumped the gun on posting, in the beginning when I was talking about writing the javascript cookie while logging in I meant it would be checked with php to verify it. That is, if it didn't exist it means the user had javascript turned off.
2:58 pm on Aug 16, 2017 (gmt 0)

New User

joined:Aug 15, 2017
posts:1
votes: 0


"Warning: Cannot modify header information - headers already sent by..." this error occurs when there is some output before <?php (for example setting the cookies using javascript)
So if you want to get rid of this error you should reorganize php scripts so there would no output before <?php
Its hard to say in details without reviewing the php code of the scripts.
5:06 pm on Aug 16, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14442
votes: 576


The only thing that has worked is, I redirected to an intermediate page

I don't think there is any alternative--until the day request headers themselves include information about scripting as such, not to be confused with your “last time I asked, scripting was off” cookie. (I do not think this is a day anyone would want to see.) By the time you know whether scripting is on or off, the user has already received the page, and it's too late to send cookies.
7:53 pm on Aug 16, 2017 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 130
votes: 2


I searched it out and messed with it intensely and finally decided on redirect (via Header(Location:) to an intermediate page where the JavaScript could be written in the head tag followed by a meta-tag re direct. The reason for the JavaScript cookie was simply to make sure JavaScript was turned on, on the users machine. The cookie then verified with php or perl (depending on the page it went to--some are .pl files and some are .php), then, if not verified the user could be redirected to the login page to start over. As mentioned initially, I wanted to make sure the timeout script would fire, hence, the reason for making sure it was turned on. Why the mixed .pl and .php files? I make no claim for a "method to my madness" here, but, it just worked out that way for the time being. Concerning the headers, as with everything, I'm sure there is a reason for the way things are, I'm in no way an expert on the subject, in fact, very far from that, but for now I rest thankful for meta tags. Thanx much for the replies, I learn so much from them.
~ a great day to all

[edited by: phranque at 9:39 pm (utc) on Aug 16, 2017]
[edit reason] Disable graphic smile faces for this post [/edit]