By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

Wierd setcookie Problem

P: 16
Hi,

I've been dealing with PHP for a few years now and I've never encountered a problem quite like this.

I've searched the net for an answer to my problem hoping somebody else may have encountered this, but without any luck, so I now resolve to pose the question myself.

Code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $somedata="Lorem Ipsum";
  3. setcookie("somename",$somedata,time()+43200,"/");
  4. ?>
  5.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

That's the everything on the relevant page. Just that. Two lines of code. No extra content or includes.
This page is not being included from any other pages and is being accessed directly.

Problem:

It doesn't work, and this is the first time setcookie hasn't worked for me without any obvious conflictions with other code. No cookie is being set.

Instead, PHP returns the following the error:

Warning: Cannot modify header information - headers already sent by (output started at /public_html/mkl/cookie.php:1) in /public_html/mkl/cookie.php on line 3

I've tried running the script over a multitude of browsers such as Firefox 1x, Firefox 2x, IE5, IE6, Mozilla, and it's the same result on all of them.

The server that the script is being executed has never given me such problems before (this is the first time setcookie hasn't worked for me like this) and as far as I'm aware has not undergone any recent changes.

The server is Apache operated and has the latest version PHP installed.

Anyone that can offer any assistance or suggestions on how I may resolve this problem would be greatly appreciated.

Thanks.
May 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: 16
Nevermind.

I've figured out what the problem was.

Always seems to go that way -> Waste hours of your life away trying to sort out a seemingly impossible problem, only to ask for help then finally figure out what's wrong for yourself... _

Incase anyone else has the same problem - I was sending the incorrect character encoding with the page header, and I'm guessing either the server, PHP or all the browsers I tested didn't like me doing that.

Anyhow, thanks for putting up with me.
May 19 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Cmaza. Welcome to TSDN!

Warning: Cannot modify header information - headers already sent by (output started at /public_html/mkl/cookie.php:1) in /public_html/mkl/cookie.php on line 3
You're getting this error because your script is sending output to the browser either before or after it tries to set the cookie.

In order for this to work, you'd have to remove all leading whitespace before the opening <?php tag, and any whitespace after the last ?> tag as well.

That's the usual culprit, at any rate. Interesting that you solved it by changing the character encoding....
May 19 '07 #3

P: 24
Hi,

I've been dealing with PHP for a few years now and I've never encountered a problem quite like this.

I've searched the net for an answer to my problem hoping somebody else may have encountered this, but without any luck, so I now resolve to pose the question myself.

Code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $somedata="Lorem Ipsum";
  3. setcookie("somename",$somedata,time()+43200,"/");
  4. ?>
  5.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

That's the everything on the relevant page. Just that. Two lines of code. No extra content or includes.
This page is not being included from any other pages and is being accessed directly.

Problem:

It doesn't work, and this is the first time setcookie hasn't worked for me without any obvious conflictions with other code. No cookie is being set.

Instead, PHP returns the following the error:

Warning: Cannot modify header information - headers already sent by (output started at /public_html/mkl/cookie.php:1) in /public_html/mkl/cookie.php on line 3

I've tried running the script over a multitude of browsers such as Firefox 1x, Firefox 2x, IE5, IE6, Mozilla, and it's the same result on all of them.

The server that the script is being executed has never given me such problems before (this is the first time setcookie hasn't worked for me like this) and as far as I'm aware has not undergone any recent changes.

The server is Apache operated and has the latest version PHP installed.

Anyone that can offer any assistance or suggestions on how I may resolve this problem would be greatly appreciated.

Thanks.
You got a right reply. You can use ob_start(); at top of the page also.

get more header info here
May 21 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.