Cookie code, whats wrong?
whats wrong with my cookie code? I am pretty sure my code SHOULD work 100%, yet it doesn't.....
Code:
<?php
if ( $_GET["RSN"] == "" || $_GET["RSN"] == "null")
{
$username = $_COOKIE["TipMeName"];
//echo '<script type="text/javascript">alert("Getting An Allready Set Cookie: '.$_COOKIE["TipMeName"].'")</script>';
} else {
setcookie("TipMeName", $_GET["RSN"] , time()+5184000 );
header( 'refresh: 0; url=/1/' );
//echo'<script type="text/javascript">alert("Setting A New Cookie: '.$_COOKIE["TipMeName"].' - '.$_GET["RSN"].'")</script>';
}
if (isset($_COOKIE["TipMeName"]))
{
echo
'
<fieldset>
<table border="0" width="100%">
<tr>
<td>
<p align="center"><font size="2">Your RS Name Is:"' .$username. '"</font><br>
<form action="#" method="get">
RSN:
<input type="text" name="RSN" size="20">
<input type="submit" value="Change It">
</form>
</p>
</td>
</tr>
</table>
</fieldset>
';
} else {
echo
'
<fieldset>
<table border="0" width="100%">
<tr>
<td>
<p align="center"><font size="2">---text goes here the i removed--
tools.</font></td>
</tr>
<tr>
<td>
<p align="center"><b><font size="2" color="#FF0000">No RS Username is set!</font></b><br>
<form action="#" method="get">
RSN:
<input type="text" name="RSN" size="20">
<input type="submit" value="Save Name">
</form>
</p>
</td>
</tr>
</table>
</fieldset>
';
}
?>
Re: Cookie code, whats wrong?
Cookies you just set aren't immediately available in the _COOKIES superglobal, only after the next browser request.
Or is your problem different? If so, tell us what it is.
Re: Cookie code, whats wrong?
Quote:
Originally Posted by CornedBee
Cookies you just set aren't immediately available in the _COOKIES superglobal, only after the next browser request.
Or is your problem different? If so, tell us what it is.
that is correct, and thats why I placed this code:
Code:
header( 'refresh: 0; url=/1/' );
I just realized that my code works perfectly fine when its on a page of its own. but once I copy and past it into my main page. it stops working.... :ehh:
Re: Cookie code, whats wrong?
did you send any output before executing that code?
once you send output to the browser, cookie and header functions cannot be executed.
Re: Cookie code, whats wrong?
Quote:
Originally Posted by kows
did you send any output before executing that code?
once you send output to the browser, cookie and header functions cannot be executed.
Can you explain a bit more?
Re: Cookie code, whats wrong?
setcookie() adds a "Set-Cookie" header to the set of headers sent to the browser. Headers must always be sent before the page content; that's a HTTP requirement.
When PHP first encounters normal content, unless content buffering is on, it sends the headers and starts the content output. Thus, after any normal content was encountered, the setcookie() function has no effect anymore.
Re: Cookie code, whats wrong?