Gossamer Forum
Home : General : Internet Technologies :

PHP: Cookies

Quote Reply
PHP: Cookies
I'm very new to using cookies in php and I was trying out some stuff on my site. What I've done is when an admin logs in, it automatically sets a cookie storing the persons username. When this then goes to the index page where the admin link is, it checks for the cookie's value. But I'm getting the following error when I try to write the cookie:

Quote:
Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\login.php:10) in c:\apache\htdocs\login.php on line 96


Here is my code for the login page.

Code:


// Checks passwords for validity.
if ($uPassword != $password) {
echo ("<h5>Error: the specified password does not match the username. <a href='login.php' style='font-size: 10px'>Retry</a>.</h5>");
} else {
echo("<h5>Thank you for logging in: " . $username . ".</h5>");

setcookie("username", "$username", time()+315360000);
?>

<center>
<form action="index.php" method="post">
<input type="hidden" readonly="true" name="username" value="<?=$username ?>">
<input type="submit" value="Continue" style="font-family: verdana" />
</form>

</center>

<?php
}
endif; ?>
And here is the code for the index page.

Code:


<?php

/***************************************************************************
* Cookie Begins.
***************************************************************************/

echo $_COOKIE["username"];
$_COOKIE['username'] = $username;


/***************************************************************************
* Cookie Ends.
***************************************************************************/

?>


Note that the echo $_COOKIE was for debugging purposes.

Any help would be much appreciated.
Quote Reply
Re: [JoFrRi] PHP: Cookies In reply to
From php.net: Cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.

Just like Perl. So try:

setcookie("username", "$username", time()+315360000);
echo("<h5>Thank you for logging in: " . $username . ".</h5>");



----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [JoFrRi] PHP: Cookies In reply to
Dan's right - cookies and any other http headers need to be called before anything is output to the browser. However, if you ever find yourself in a situation where it's unavoidable that you send http headers after already having sent output, you can get around this restriction by using output buffering.

In other words, the following works without raising any errors:

Code:
ob_start();
echo 'This is some content';
setcookie(...);
ob_end_flush();

This works with PHP 4 and later.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] PHP: Cookies In reply to
Thanks guys.

What I ended up doing, is creating a specific page just for my cookies. I have once a person's username and pword have been authorized, they are forwarded to my login_cookie page, here I only use 3 lines, 2 cookies are placed, and a header() is used to forward the page to my index. Works beautifully so far.