Solved

Conflict between SetCookie() and include ?

Posted on 2000-04-04
7
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:stephaneeybert
Comment Utility
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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now