473,791 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wierd setcookie Problem

16 New Member
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
3 2295
Cmaza
16 New Member
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
5,821 Recognized Expert Expert
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
satya61229
24 New Member
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

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

Similar topics

16
11320
by: Phil Powell | last post by:
Fourth attempt.. it fails now in login, I check by printing $_COOKIE and there is no value there! Guys, what on earth do I do about this???? Here is the code that sets the cookie: if ($hasLoggedIn && ($row = mysql_fetch_row($query))) { setcookie('nordicnet_registration', $row, 0, '/'); @mysql_free_result($query);
5
6463
by: Ben | last post by:
Hi all, In my .php file, I'm using both session_start() and setcookie() before <html> tag. It gives me following warning message: Warning: Cannot modify header information - headers already sent by (output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148 My .php file looks like this:
3
2874
by: Markus Fischer | last post by:
Hi, I'm experiencing a wierd problem with IE 6 in Windows with two _slightly_ different Version. Give the following HTMl-Code, ideally the output of offsetTop should be "105"; a few pixel plus minus would still be ok, whyever. I've tested this successfully on 6.0.2800.1106 on a german W2KPro machine, also with 6.0.2800.1106 on a german XPPro machine.
0
1120
by: Michael | last post by:
Hi, I found a wierd problem in DataGrid. If I set DataGrid's DataSource to empDataSet1 at designtime, then I can never change its DataSource at runtime, e.g., in the Button1_Click event procedure: DataGrid1.DataSource = dataView1; DataGrid1.DataBind(); But if I clear DataGrid's DataSource property at designtime,
6
2077
by: GregoryD | last post by:
I'm going to try to simplify this issue as much as possible. In my public_html/ directory, I have a login script that sets a cookie to be used for authentication purposes. To set the cookie, it has something like this: <?php setcookie('code',$value, time + $lifetime, '/', '.example.com'); ?>
9
2298
by: LayneMitch via WebmasterKB.com | last post by:
Hello. Got another one for you folks. I'm working on this problem that wants me to 1. Prompt for name 2. Use pop-up box with name 3. Display current date on page in format "October 30, 2000." 4. Display last modified date of doc. Here is my attempt. What a headache :-(
8
5872
by: SupraFast | last post by:
I have two hosting accounts. On one, my setcookie script works fine; cookies are created. On the other, the same script doesn't work. The function returns TRUE, but no cookies is created. I checked to make sure that the variables have values and that the proper expire time is set and etc. Any ideas? <?php foreach($_POST as $name => $value){ if($name == 'hItem'){ $item = $value;
9
26258
by: wangers16 | last post by:
Hi, I am quite new to PHP and recently I have been attempting to create a login script, just one problem, the setcookie function isn't working. I have tried a basic php file with nothing other than a function to set and retrieve the cookie but still nothing. Why is this happening?
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.