cookie question

A

Anonymous

Guest
Hi all,

there is a rule that you have to set a cookie before you generate any html code. But what do you have to do, when at the beginning of the page you have a STYLE block, and that you later on in the page wants to set a cookie.
It then gives an error when the cookie is set, because the style block generated HTML code.

What can I do ?
 
-[Bl@de Runner said:
-"]Hi all,

there is a rule that you have to set a cookie before you generate any html code. But what do you have to do, when at the beginning of the page you have a STYLE block, and that you later on in the page wants to set a cookie.
It then gives an error when the cookie is set, because the style block generated HTML code.

What can I do ?

Interesting, what do you mean about STYLE block? CSS?
I think, what if you set cookie, and after what change code (some variable) cause PHP will generate Error...
 
Hello Balade runner,

I think you must do like this...


Code:
<?
setcookie ( names, vars, etc.)

echo("<html>");

echo("<head>");
// "style block" =) here
echo(</head>);

echo("<body></body>")


?>
 
-[Bl@de Runner said:
-"]Hi all,

there is a rule that you have to set a cookie before you generate any html code. But what do you have to do, when at the beginning of the page you have a STYLE block, and that you later on in the page wants to set a cookie.
It then gives an error when the cookie is set, because the style block generated HTML code.

What can I do ?

Cookies HAVE to be set before anything else is sent to the browser because the command to set the cookie is sent in the headers of the page. If you're generating a single page there should be no reason why you can't set the cookie before anything else.

It's also worth pointing out that you cannot use the cookie value on the same page immediately after you set it. This is because when you select a new page the browser sends the cookie data with it.

If you want to set a cookie AND use the cookie variable, then set the variable first, and use this variable to set the cookie and use later in the page.
 
if you want to set a cookie AFTER you printed something in you're page. You can use this command BEFORE you start printing:
Code:
ob_start();
this will start buffering of the page and it will not be sent until the script is finished.
you use this command to start sending the page before the script is finished (buffer will be sent + anything you print to the page after that.)
Code:
ob_flush();
you can also set buffering of you're page on by default in the php-configuration, but that's a little heavyer for you're server.

if you can set you're cookies before you print anything to you're page you should do that, as you're server will like that [:)]

Greetz Daan
 
You must only send headers before any characters out of <? ?> tags and before any output within <? ?> tags
------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
www.AlarIT.com
 
Back
Top