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.
Subject Author Views Date
Thread PHP: Cookies JoFrRi 4496 Sep 2, 2003, 11:08 PM
Post Re: [JoFrRi] PHP: Cookies
dan 4333 Sep 2, 2003, 11:17 PM
Thread Re: [JoFrRi] PHP: Cookies
hennagaijin 4303 Sep 7, 2003, 6:55 AM
Post Re: [hennagaijin] PHP: Cookies
JoFrRi 4277 Sep 8, 2003, 11:23 AM