[RESOLVED] What's wrong with setcookie in my code?
Code:
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\game.php:6) in C:\AppServ\www\Codes\Login.php on line 12
PHP Code:
<?php require("Config.php"); ?>
<?php
mysql_connect($Connection, $Username, $Password) or die("Cannot connect to MySQL Server.");
mysql_select_db($Database) or die("Cannot find Database.");
$login = mysql_query("DELETE FROM sp_accounts WHERE Username=''");
?>
<?php
mysql_connect($Connection, $Username, $Password) or die("Cannot connect to MySQL Server.");
mysql_select_db($Database) or die("Cannot find Database.");
$login = mysql_query("SELECT * FROM sp_accounts WHERE Username='$_POST[Username]' AND Password='$_POST[Password]'");
if(mysql_affected_rows() == 1) {
setcookie("Username", $_POST['Username'], time() + 8000);
echo "Login: OK!";
echo "<br />"; }
if(!isset($_cookie["Username"]) && !isset($_cookie["Password"])) {
echo ("We can't find cookie: ");
die('<a href="login.php">Try again.</a>');
}
?>
Thank you for incoming answers.
Re: What's wrong with setcookie in my code?
setcookie() sends a header to the browser. headers aren't allowed to be sent once any output is sent to the browser. this means -any- output at all. for example, the following line of code from your exerpt:
will produce a line break. that very well could be your problem. from PHP.net:
Quote:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, 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.
however, after a quick glance over your code, it looks like you've just taken a rather large chunk and then chopped a bunch off (you're missing closing braces, you only have one call to setcookie() but you're checking for two cookies that are set, etc). so, you could be echoing out actual mark-up (HTML) before you're setting the cookie, or something. so, make sure you're not outputting anything before you send the header.
you should also note that setcookie() doesn't set a variable in the $_COOKIE super global until the next page request. so, if you're making a login system that first checks if the login is okay, and then sets a cookie, and then later on checks if the cookie is set, the cookie will look as if it's not set until you refresh the page or browse to another page. from PHP.net:
Quote:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.
Re: What's wrong with setcookie in my code?
*edit* Kows beat me to it, he also had to one up me going more in depth than I did. You win this round.
That error means you're attempting to modify header information after headers have already been sent to the browser.
Do you have any output on screen before you do setcookie? If so, then header information has already been sent and you cannot modify the cookie value.
Re: What's wrong with setcookie in my code?
Hmm hmm. I'm confused :\
I don't still understand where i need to put that setcookie or WHY i can't put it. I understood something like i must put it before.
E: Before EVERYTHING...:O