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:
Here is my code for the login page.
// 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.
<?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:
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 96Here 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; ?>
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.