Click to See Complete Forum and Search --> : Damn Cookie Problem!


ownedman
07-01-2005, 05:45 PM
well i know im not wanting this document to be html compliant and i know i got a meta tag ion the middle of my page :P
i just dont know wats rong?!?!?
it gave me this on cookie.php
Warning: Cannot modify header information - headers already sent by (output started at C:\Site\tests\cookie.php:16) in C:\Site\tests\cookie.php on line 27

line 27 ill comment in the script. lol its the setcookie line.

cookie.php

<?
session_start();
header('Cache-control: private');
$self = $_SERVER['PHP_SELF'];
?>
<html>
<head>
<title>Cookie Test</title>
</head>
<body>
<center>
<h1>My Cookie Test :P</h1>
<p>&nbsp;</p>
<h3>Ener your name to be set to the cookie.</h3>
<br />
<form method="post" action="<?= $self ?>">
Enter Your Name >> <input type="text" name="value">
<br />
Thnx.
<input type="hidden" value="http://154.20.148.186:1716/tests/cooktest.php" name="url">
<input type="submit" value="Set Cookie">
</form>
<p>&nbsp;</p>
<?
$value = $_POST['value']
if(isset($_POST['value'])) {
setcookie("cookie", $value, time()+3600, "/tests/", "http://154.20.148.186:1716", 0); //line // 27
?>
<span style="color:#FF0000">Cookie Set Redirecting......</span><br />
<meta http-equiv="refresh" content="5;url=redirect.php">
</center>
</body>
</html>
<?
};
?>
<?
$url = $_POST['url'];

if(!isset($url)) {
unset($url);
echo "enter sumthin.<br />";
} else {
$_SESSION['url'] = $_POST['url'];
};
?>


i dont think anythings wrong with this. but ima gonna post all three pages just in case.

redirect.php


<?
session_start();
header('Cache-control: private');
if(!isset($_SESSION['url'])) {
echo "get lost!";
} else {
?>
<center>
<p>&nbsp;</p>
<h1>Redirecting....</h1>
<meta http-equiv="refresh" content="5;url=<?= $_SESSION['url'] ?>">
</center>
<?
};


and here is the page that tests to see if the cookie was placed.

cooktest.php


<?
session_start();
header('Cache-control: private');
if (isset($_SESSION['url'])) {
$_SESSION = array();
session_destroy();
};

if (isset($_COOKIE['cookie'])) {
echo "<p>&nbsp;</p><center><h1> OORAA! the cookies is set!</h1<br /><strong> hello " .$_COOKIE['cookie']. "!</strong><p>&nbsp;</p>";
} else {
echo "<p>&nbsp;</p><center>Cookie failed to set :(<br /> <p>&nbsp;</p>";
};

print_r($_COOKIE);

echo "<br />";

if(isset($_COOKIE['cookie'])) {
?>
<h4>Delete Cookie?</h4><br />
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<input type="submit" value="Delete Cookie" name="delete">
<br /> <strong>Note:</strong>if you do not delete the cookie it will expire in one hour.<br />
</form>
<?
};

$delete = $_POST['delete'];

if ($delete == TRUE) {
setcookie("cookie", "", time() - 3600, "/tests/", "http://154.20.148.186:1716");
echo "Cookie Deleted.";
} else {
unset($delete);
};
?>

<br />
thanks for viewing my script i used sessions and cookies enjoy :P<br /> try again? <a href="cookie.php">Click</a>.
<br />
copyright&copy; Richard C-Wilson
</center>


thanks alot!

ownedman
07-01-2005, 05:48 PM
script located at http://154.20.148.186:1716/tests/cookie.php

NogDog
07-01-2005, 10:02 PM
The setcookie() call must be made before anything gets output to the browser, including HTML text not inside of your PHP tags. Just move the code with the cookie stuff up to the initial block of PHP code, and that problem should be taken care of.