?
Solved

Conflict between SetCookie() and include ?

Posted on 2000-04-04
7
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Industry Leaders: 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

741 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