|
-
Dec 6th, 2005, 10:29 AM
#1
Thread Starter
Hyperactive Member
PHP Cookie Help
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 Code:
<?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>";
}
}
?>
Last edited by 308holes; Dec 6th, 2005 at 12:12 PM.
Reason: code fix
-
Dec 6th, 2005, 05:55 PM
#2
Re: PHP Cookie Help
What are the messages you are receiving?
-
Dec 6th, 2005, 06:57 PM
#3
Fanatic Member
Re: PHP Cookie Help
When you set a cookie, it has to be set before any data is acctually sent.
-
Dec 7th, 2005, 05:36 AM
#4
Thread Starter
Hyperactive Member
Re: PHP Cookie Help
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
-
Dec 7th, 2005, 05:40 AM
#5
Re: PHP Cookie Help
Exacactlly what k1ll3rdr4g0n said
You can't write anything to your webpage b4 you set your cookie.
PHP Code:
//Will work
setcookie("ShowOrHide", $ONE);
echo "Cookie set";
//Won't work
echo "About to set cookie";
setcookie("ShowOrHide", $ONE);
-
Dec 7th, 2005, 06:08 AM
#6
Thread Starter
Hyperactive Member
Re: PHP Cookie Help
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 ?
-
Dec 7th, 2005, 06:43 AM
#7
Thread Starter
Hyperactive Member
Re: PHP Cookie Help
ok im lost
html code:
PHP 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:
PHP Code:
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;
}
-
Dec 7th, 2005, 07:11 AM
#8
Hyperactive Member
Re: PHP Cookie Help
<!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>
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Dec 7th, 2005, 08:00 AM
#9
Thread Starter
Hyperactive Member
Last edited by 308holes; Dec 7th, 2005 at 08:17 AM.
-
Dec 7th, 2005, 08:47 AM
#10
Thread Starter
Hyperactive Member
Re: PHP Cookie Help
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 ?
-
Dec 7th, 2005, 10:43 AM
#11
Re: PHP Cookie Help
A cookie is sent to the client via the HTTP header in the HTTP response. A typical HTTP response may look like this:
Code:
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.
-
Dec 7th, 2005, 11:49 AM
#12
Thread Starter
Hyperactive Member
Re: PHP Cookie Help
Cool thanks for the example :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|