[SOLVED] Cookies will not set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
javman
Forum Newbie
Posts: 5
Joined: Sat Jun 19, 2004 2:19 am

Cookies will not set

Post by javman »

I have the following code which will not work
<?php
echo "Hello World";
$string_name = "highspeed";
$string_value ="test";
$expiry_info = time() +40000;
setcookie($string_name, $string_value, $expiry_info);
?> the hello world works but it will not set a cookie.
If I use Cold Fusion the cookie sets right away.
Why will the php cookie not set?

Thanks very much for any help as this is my first PHP code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try moving the echo line after setcookie()..
javman
Forum Newbie
Posts: 5
Joined: Sat Jun 19, 2004 2:19 am

Post by javman »

That worked --!!thanks
However I am still confused. When i used the code alone without any html it works now. But when I use the php code in a PLAIN html page named
faith.php it will not set a cookie. This is confusing. And frustrating.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PHP Trial</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php 
$string_name = "highspeed"; 
$string_value ="test"; 
$expiry_info = time() +40000; 
setcookie($string_name, $string_value, $expiry_info); 
echo "Hello World";
?> 
</body>
</html>
feyd|use

Code: Select all

tags when posting code please.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you are printing information, in this case HTML prior to the cookie getting set. Read the tutorial (in the Tutorials section) about header errors..
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Header errors are very irratating especially when you are new to PHP. Try this:

Code: Select all

<?php
$string_name = "highspeed";
$string_value ="test";
$expiry_info = time() +40000;
setcookie($string_name, $string_value, $expiry_info);
echo "Hello World";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PHP Trial</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
HTML CODE HERE. NOT THE COOKIE SET. IT WOULD RESULT IN A HEADER ERROR AS  THERE IS OUTPUT BEEN GIVEN!
</body>
</html>
Take feyd's suggestion and goto the tutorials section!


All the best



Joe 8)
javman
Forum Newbie
Posts: 5
Joined: Sat Jun 19, 2004 2:19 am

Post by javman »

Many thanks
Actually that is what I did to get it working. but I did not know why it would not work . Your comments have been very helpful.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

ob_start();

:wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

...is a crutch.
Post Reply