PDA

Click to See Complete Forum and Search --> : PHP Cookie Help


308holes
Dec 6th, 2005, 09:29 AM
hello got this php script i wrote to diplay stuff depending on the cookie value but getting errors on the setcookie anyone know why or can improve on the code please doo


<?php
if($_GET['CIEC'] == 3)
{
$TWO = "2";
setcookie("ShowOrHide",$TWO);
}
else
{
if (isset($_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "1")
{
echo "<a href=\"index.php?CIEC=3\">Close</a>";
}
else if(isset($_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "2")
{
//Do Nothing
}
else
{
$ONE = "1";
setcookie("ShowOrHide", $ONE);
echo "<a href=\"index.php?CIEC=3\">Close</a>";
echo "<a id=\"tooCool\" href=\"http://www.w3.com/\"></a>";
}
}
?>

lintz
Dec 6th, 2005, 04:55 PM
What are the messages you are receiving?

k1ll3rdr4g0n
Dec 6th, 2005, 05:57 PM
When you set a cookie, it has to be set before any data is acctually sent.

308holes
Dec 7th, 2005, 04:36 AM
im getting


Warning: Cannot modify header information - headers already sent by (output started at htdocs\index.php:9) in htdocs\index.php on line 143

lintz
Dec 7th, 2005, 04:40 AM
Exacactlly what k1ll3rdr4g0n said :D

You can't write anything to your webpage b4 you set your cookie.

//Will work
setcookie("ShowOrHide", $ONE);
echo "Cookie set";

//Won't work
echo "About to set cookie";
setcookie("ShowOrHide", $ONE);

308holes
Dec 7th, 2005, 05:08 AM
ok I think I see what your saying but i am not writing anything untill the php checks to see if the cookie is there and what value it has could it be because the css info for that tag is being processed before the cookie if soo how do i get around that ?

308holes
Dec 7th, 2005, 05:43 AM
ok im lost

html code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="main.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<?php
if($_GET['CIEC'] == 3)
{
$TWO = "2";
setcookie("ShowOrHide",$TWO, time()+604800);
}
else
{
if (isset($_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "1")
{
echo "<a href=\"index.php?CIEC=3\">Close Cool IE</a>";
echo "<a id=\"tooCool\" href=\"http://www.w3junkies.com/toocool/\"></a>";
}
else if(isset($_COOKIE["ShowOrHide"]) AND $_COOKIE["ShowOrHide"] == "2")
{
//Do Nothing
}
else
{
$ONE = "1";
setcookie("ShowOrHide", $ONE);
echo "<a href=\"index.php?CIEC=3\">Close Cool IE</a>";
echo "<a id=\"tooCool\" href=\"http://www.w3junkies.com/toocool/\"></a>";
}
}
?>
</body>
</html>


CSS:

a#tooCool {
position: fixed;
right: 0;
bottom: 0;
display: block;
height: 80px;
width: 80px;
background: url(imgs/too_cool_corner.png) bottom right no-repeat;
text-indent: -999em;
text-decoration: none;
}

PlaGuE
Dec 7th, 2005, 06:11 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="main.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>

308holes
Dec 7th, 2005, 07:00 AM
Ok think i got it now

308holes
Dec 7th, 2005, 07:47 AM
ok go the code working now works good just for g wiz info there is no ways to set the cookie from inside the html code like above code ?

visualAd
Dec 7th, 2005, 09:43 AM
A cookie is sent to the client via the HTTP header in the HTTP response. A typical HTTP response may look like this:

HTTP/1.1 200 OK
Date: Wed, 07 Dec 2005 15:38:00 GMT
Server: Apache
Set-Cookie: RMID=50b16804439701d0; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.vbforums.com bblastvisit=1133969880; expires=Thu, 07-Dec-06 15:38:00 GMT; path=/; domain=.vbforums.com bblastactivity=1133969868; expires=Thu, 07-Dec-06 15:38:00 GMT; path=/; domain=.vbforums.com
Expires: 0
Cache-Control: private, post-check=0, pre-check=0, max-age=0
Pragma: no-cache
Content-Length: 112655
Content-Type: text/html; charset=ISO-8859-1
Proxy-Connection: close

<html>
<head>
</head>
<body>
<!-- page content here -->
</body>
</html>

This is the response you get from vbforums when you first go to the site. The set cookie string is highlighted. As you can see it is a header and appears before the main body of the page.

In PHP the set_cookie function simply adds one of these header. However, the second you produce any output the headers are flushed through and the body of the HTTP response is started.

You can get around this by using output buffers, which collect all the data in a buffer before sending it to the client. However, it is bad practice to do this. A well designed script will keep all output separate from the scripts processes.

308holes
Dec 7th, 2005, 10:49 AM
Cool thanks for the example :-)