Solved

Sending a Cookie to the Server, First.

Posted on 2002-05-09
8
257 Views
Last Modified: 2008-03-03
Hi Guys $ Ladies,

I am confused here.  I keep getting header errors when I try to set a cookie.  Then I realise that I have to Send the cookie to the server first.  ........  hha ha ha, got it.  I wipe everything off my folder and upload the cookie page first.  Still it does not work.

What is meant by sending the cookie first?, and how do I have to go about it.?
0
Comment
Question by:Senyonjo
8 Comments
 
LVL 4

Expert Comment

by:lokeshv
ID: 7000354
0
 
LVL 6

Accepted Solution

by:
andreif earned 50 total points
ID: 7000606
when you set headers using
header() or setcookie()
you can meet an error which says that headers are already sent.
This means that your script is not allowed to produce any output before header() or setcookie() call.

Sometimes it can be empty line like this:

--------- php file begin ------------

<?php

header("dsf");

?>

this empty line before php tag will send to output before header call, so you can't modify headers any more
0
 
LVL 2

Expert Comment

by:jpoesen
ID: 7000637
(This answer repeats some of andreif's comments)

you can't send header info manualy after some header info has already been sent.

It doesn't have anything to do with uploading files or anything.

Basically, when you write a page that starts with <html>, header info will be sent, this is how http works.

When you use a
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:jpoesen
ID: 7000643
sorry.

when you use a cookie function like this :

setcookie('myCookieName', $myCookieValue,time()+15000);
Header("Location: somepage.php");

you send header info, namely "Location:somepage.php".

If you would do this :
<html>
<body>
sending cookie
<?
setcookie('myCookieName', $myCookieValue,time()+15000);
Header("Location: somepage.php");
?>
</body>
</html>

you automatically send header info because you have html to display, but then you request to send more info, after the header info has already been sent.

Like andreif said, even a blank line or a comment can be enough to trigger Header info to get sent.

In short : when use a Header() function, make sure that there is *no* possibility of characters being sent, like blank lines or comments.

Good luck
jpoesen

if you still have problems, post your code.
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7002682
If you need to set the cookie after the start of your script, like jpoesens code

<html>
<body>
<?
setcookie("myCookie","Value");
?>

you could use ob_start() and ob_end_flush() to buffer the output:

<?
ob_start();
?>
<html>
<body>
<?
setcookie("myCookie","Value");
ob_end_flush();
?>

This would work.

So you have to choice.

The most common one is to make sure that all cookies are set at the beginning of the script as the other comments describes to you.

Or: You could buffer the output to somewhere down in the script. Then the content of the page will be buffered on the server until ob_end_flush() is called.

Batalf
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7005681
Just another point to add.

A blank line at the top of a file before the <?php tag will be enough to bugger up cookies if you are not using on_start() and ob_end_flush().

Regards,

Richard Quadling.
0
 

Author Comment

by:Senyonjo
ID: 7018446
Thise were exellent comments.  Very helpful and my project is moving forward.  Thank you
0
 
LVL 1

Expert Comment

by:Kriek
ID: 8181703
In addition to Batalf's comment

ob_end_flush() isn’t needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start(). I should also mention that Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
is this a cms? 8 60
.htaccess 5 36
does post require a form or curl to be post 4 29
Add email address from a web page to outlook 2010 contacts ? 8 23
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

776 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