<!-- m --><a class="postlink" href="http://24.238.145.68/public/game.phps">http://24.238.145.68/public/game.phps</a><!-- m --> <-- code
<!-- m --><a class="postlink" href="http://24.238.145.68/public/game.php">http://24.238.145.68/public/game.php</a><!-- m --> <--- actual game
ok, so the basic mechanics of the game are working, but i have a problem. i want it so that when the user guesses correctly, the cookie is deleted. when i try this, however, i get an error message about the header being blah blah blah.
how can i do this?I don't see anywhere where you are deleting a cookie? I see 2 cookie creations, but no deleteons. and those are 2 different cookie you have there.yeah, i took out the cookie deletion code cuz it was causing the error.
it was here:
if ($guess==$num) {
echo "RIGHT ON DUDE!";
setcookie(name, $rand, time() - 3600);
}
thats how you delete them right? yes but take the echo out. that is casuing the error.why would echo cause an error???
well, now I've got:
<?
$rand = rand(1,1000);
if($_COOKIE['name']==NULL) {
setcookie(name, $rand, time()+3600);
}
if($_COOKIE['guesses']==NULL) {
$guessnum = 0;
setcookie(guesses, $guessnum);
}
$num = $_COOKIE['name'];
?>
<html>
<body>
<form action="game.php" method="post">
<input type="text" name="guess" value="Guess">
<input type="submit">
</form>
<form action="del.php" method="post">
<input type="submit" value="reset">
</form>
<?
$guess = $_POST['guess'];
if ($guess < $num) {
echo "Higher!";
}
if ($guess > $num) {
echo "Lower!";
}
if ($guess==$num) {
setcookie(name, $rand, time() - 3600);
}
?>
Warning: Cannot add header information - headers already sent by (output started at c:\phpdev\www\public\game.php:24) in c:\phpdev\www\public\game.php on line 36it causes an error because you cant set a cookie after a header has been sent, you have to set it before any output or before you use header().hm...
could i use sessions instead of cookies?
don't they work basically the same way?
can you set a session variable in the body?why cant you just put all the conditional stuff at the top and store the echo stuff in a variable and output it later? ;P
you could make an output buffer with ob_start() or whatever it is, but there really isnt a reason to in this.Originally posted by Gregory
why would echo cause an error???
because, just like the error states, you already sent something to the browser to print. you cannot send anything , not even <html> tag or a even a space. you have to setcookie at the top before anything is sent to the browser. yes you can use session which will be the best thing for this type of script. you can set a session anywhere you want but you have to have session_start() on all of your pages that you want to use sesions on. and it has to be the frist line after <?php that.
<!-- m --><a class="postlink" href="http://24.238.145.68/public/game.php">http://24.238.145.68/public/game.php</a><!-- m --> <--- actual game
ok, so the basic mechanics of the game are working, but i have a problem. i want it so that when the user guesses correctly, the cookie is deleted. when i try this, however, i get an error message about the header being blah blah blah.
how can i do this?I don't see anywhere where you are deleting a cookie? I see 2 cookie creations, but no deleteons. and those are 2 different cookie you have there.yeah, i took out the cookie deletion code cuz it was causing the error.
it was here:
if ($guess==$num) {
echo "RIGHT ON DUDE!";
setcookie(name, $rand, time() - 3600);
}
thats how you delete them right? yes but take the echo out. that is casuing the error.why would echo cause an error???
well, now I've got:
<?
$rand = rand(1,1000);
if($_COOKIE['name']==NULL) {
setcookie(name, $rand, time()+3600);
}
if($_COOKIE['guesses']==NULL) {
$guessnum = 0;
setcookie(guesses, $guessnum);
}
$num = $_COOKIE['name'];
?>
<html>
<body>
<form action="game.php" method="post">
<input type="text" name="guess" value="Guess">
<input type="submit">
</form>
<form action="del.php" method="post">
<input type="submit" value="reset">
</form>
<?
$guess = $_POST['guess'];
if ($guess < $num) {
echo "Higher!";
}
if ($guess > $num) {
echo "Lower!";
}
if ($guess==$num) {
setcookie(name, $rand, time() - 3600);
}
?>
Warning: Cannot add header information - headers already sent by (output started at c:\phpdev\www\public\game.php:24) in c:\phpdev\www\public\game.php on line 36it causes an error because you cant set a cookie after a header has been sent, you have to set it before any output or before you use header().hm...
could i use sessions instead of cookies?
don't they work basically the same way?
can you set a session variable in the body?why cant you just put all the conditional stuff at the top and store the echo stuff in a variable and output it later? ;P
you could make an output buffer with ob_start() or whatever it is, but there really isnt a reason to in this.Originally posted by Gregory
why would echo cause an error???
because, just like the error states, you already sent something to the browser to print. you cannot send anything , not even <html> tag or a even a space. you have to setcookie at the top before anything is sent to the browser. yes you can use session which will be the best thing for this type of script. you can set a session anywhere you want but you have to have session_start() on all of your pages that you want to use sesions on. and it has to be the frist line after <?php that.