Solved

Conflict between SetCookie() and include ?

Posted on 2000-04-04
7
202 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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 50 total points
ID: 2688889
thank you
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

696 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