Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

cookie question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

cookie question

Postby -[Bl@de Runner]- » Sat Jul 06, 2002 3:34 am

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]-
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sun Jun 30, 2002 6:45 am

Re: cookie question

Postby WiZARD » Sat Jul 06, 2002 4:46 am

-[Bl@de Runner]- wrote: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...
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Oleg Butuzov » Sat Jul 06, 2002 6:33 am

Hello Balade runner,

I think you must do like this...


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

echo("<html>");

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

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


?>
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sat Jul 06, 2002 6:35 am

oph sorry , I wrote you name wrong... I so sorry... =)

regards...
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Re: cookie question

Postby Jay » Fri Aug 02, 2002 11:27 am

-[Bl@de Runner]- wrote: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.
Jay
 

Postby DoppyNL » Mon Aug 05, 2002 9:41 am

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: Select all
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: Select all
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
DoppyNL
 

Postby serge » Mon Sep 16, 2002 1:44 am

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
serge
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Sep 16, 2002 1:25 am
Location: Ukraine


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron