Php setcookie()

Hi All,

I’m just starting out with PHP and am working on setting cookies. Here is the code I enter when I get a “Warning: Cannot modify header information - headers already sent by (output started at /home/unknown/public_html/julian/phpcookie.php:8) in /home/unknown/public_html/julian/phpcookie.php on line 10":

<?php $value = 'Datapimp'; setcookie ("username", $value,time()+3600, "/julian/", "unknowndesigns.net", 0); ?> 
</body>

I’m sure it’s just my n00b PHP mind. :stuck_out_tongue:

Thanks in advanced,
-Data :sketchy:


<?php

setcookie("username", "Datapimp", time()+3600, etc..., etc...);

?>

should work. You have to set the cookie before everythng else…

<?php



setcookie("username", "Datapimp", time()+3600, "/julian/", "unknowndesigns.net", 0);



?>

Server Returns:

Warning: Cannot modify header information - headers already sent by (output started at /home/unknown/public_html/julian/php/phpcookie.php:8) in /home/unknown/public_html/julian/php/phpcookie.php on line 8

-Data :sketchy:

where are you placing this on your page? It has to be the first thing on your page…


<?php
setcookie();
?>

<html>
everthing else here....
?>

Exact HTML:

<html>
<?php



setcookie("username", "Datapimp", time()+3600, "/julian/", "unknowndesigns.net", 0);



?>
<head>
<title>PHP Cookie</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
</body>
</html>

no you have to put HTML after the PHP


<?php
setcookie("username", "Datapimp", time()+3600, "/julian/", "unknowndesigns.net", 0);
?>
<html>
<head>
<title>HP Cookie</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
</body>
</html>

PHP has to go before everything when you are setting cookies and using sessions…

Wow thanks a bunch Jubba it works!

When I use

<?php
echo $_COOKIE["username"];
echo $HTTP_COOKIE_VARS["username"];

Do I put it in the header like the setcookie() or in the body?

that can go anywhere in the body.

and use $_COOKIE[] because the other one is for the other versions of PHP…

also is most of the new servers that have updated their php you can simply use “$username” and it will print out the cookie value…

Well I got it all working but now I have one more question.

How would I go about setting the cookie var that is currectly “Datapimp”. I am trying to make a login script in PHP so that the cookie content is inserted by a input box.

I’ve been looking around php.net as how to do that but maybe you know off hand.

Thanks again Jubba.
-Data :sketchy:

then you would just do this:


&lt;?
setcookie("username", $_POST['textboxVariable'], etc, etc...);
?&gt;