?
Solved

Conflict between SetCookie() and include ?

Posted on 2000-04-04
7
Medium Priority
?
206 Views
Last Modified: 2013-11-18
Dear All,

I would like to know why I cannot use the SetCookie() function after having used the include directive in a PHP page.

If I use the SetCookie() function before using the include directive, I don't get any error message. But if I use it after, I get a message saying that the html header has already been sent. I know that the SetCookie() function must be used AFTER using the <HEAD> html tag. But how come it also must be used after using the include directive?

Kind Regards
Stephane
0
Comment
Question by:stephaneeybert
  • 3
  • 3
7 Comments
 

Expert Comment

by:mrvithan
ID: 2686183
According to PHP manual, it say cookie must be sent before any other headers are sent. it means this requires you to place calls this function before any <HTML> or <HEAD> tag.

I think you can place it after <HTML> might be a bug.... It get the same error with session on PHP4
0
 
LVL 1

Expert Comment

by:mafweb
ID: 2686390
the setcookie() function (session() equally) adds information to the http header that is sent before _any_ content. This is like typing a command on your command line, after you've pressed enter, there is no way of altering the parameters. So you cannot send the cookie information after the <header> tag. Maybe there is a confusion here between the HTML <HEADER> tag and the HTTP header containing the cookie
Be especially aware of any whitespaces and newlines outside the <?php ?>tags

maf
0
 

Author Comment

by:stephaneeybert
ID: 2686701
Here is what I do:

<?php

include myfile.inc

?>

<?php

setcookie(...);

print ("<HTML><HEAD><BODY>");
....

?>

And it does not work.

If I remove the include directive, then it works.

Cheers
Stephane
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:stephaneeybert
ID: 2686705
What is it with the white spaces and new lines outside of the php tags?
What should I do?
Cheers
Stephane
0
 
LVL 1

Expert Comment

by:mafweb
ID: 2687079
have a look at the line between the ?> an the <?

                      include myfile.inc

                      ?>

                      <?php

                      setcookie(...);

try

<?
 include myfile.inc

// no ?> <? here

setcookie(...);

?>

you may not output any characters (that's what the line actually does) before setcookie()

maf
0
 

Author Comment

by:stephaneeybert
ID: 2688878
Hi maf,
It was white spaces after the ?> in the included files that posed problem. I removed them, and now it's fine. You got your points.
Thanks
Stephane
0
 
LVL 1

Accepted Solution

by:
mafweb earned 150 total points
ID: 2688889
thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question