[php] Cannot modify header info
Hey. I've been searching a bit and can't find what I want. I've been trying to fix this for a while now. I keep getting this error:
Warning: Cannot modify header information - headers already sent by... (bla bla)
I've been trying to set a cookie.
Here's the full script:
Code:
<?php
if ($page == "login"){
if (!isloggedin()){
echo "<center><br><font size='4'><b>Login Account</b></font><br><br>";
setcookie("id","0");
setcookie("user","Nuklear");
setcookie("pwd","password");
}else{ echo "<center><br>"; $error = 23; include("error.php"); exit;}
}
?>
Can someone please point out my mistake?
Re: [php] Cannot modify header info
That error generally appears when you try to send a header (header();) while you already outputed something (ie. echo();).
What does error.php contains? The function header(); perhaps? Or in the function isloggedin();, or perhaps above this code (if there is something above you didn't posted).
PS: You need to set ob_start(); before cookies work...
Re: [php] Cannot modify header info
Quote:
PS: You need to set ob_start(); before cookies work...
Fail. Short time hacks is never a worthy solution.
It's simple, don't output anything before session_start() , cookies, headers of any format, etc.
Obviously the error
PHP Code:
echo "<center><br><font size='4'><b>Login Account</b></font><br><br>";
setcookie("id","0");
setcookie("user","Nuklear");
setcookie("pwd","password");
Which just needs to be
PHP Code:
setcookie("id","0");
setcookie("user","Nuklear");
setcookie("pwd","password");
echo "<center><br><font size='4'><b>Login Account</b></font><br><br>";
Re: [php] Cannot modify header info
DeathArt solved it. :P Thanks. xD
Re: [php] Cannot modify header info
If the error comes up again, because you may modify it in the future, make sure you remove any blank space before setting the cookie, as this can sometimes cause problems.
Re: [php] Cannot modify header info
For me it never works till I use ob_start(); :S.
Re: [php] Cannot modify header info
Lol, the problem was just that you used html(any graphics) befor setting the cookie, not about blank spaces , rofl.
Re: [php] Cannot modify header info
Quote:
Originally Posted by
Mario_Party
If the error comes up again, because you may modify it in the future, make sure you remove any blank space before setting the cookie, as this can sometimes cause problems.
Indeed, this is usually what I mess up on. My organization is horrible by other's standards and it makes things like this happen lol
Quote:
Originally Posted by
nrak
Lol, the problem was just that you used html(any graphics) befor setting the cookie, not about blank spaces , rofl.
Basically, but what he means is blank space before the <?php tag which basically comes to html (you're right and wrong).
This is usually my biggest problem as well ^_^
It's just a warning, I think, so I don't think it dies on it. Correct me if I'm wrong.
Re: [php] Cannot modify header info
Quote:
Originally Posted by
nrak
Lol, the problem was just that you used html(any graphics) befor setting the cookie, not about blank spaces , rofl.
As Wh005h says you are right and wrong. Although I know you are trying to help, please learn the facts before telling people the wrong things, as learning several different things is only more confusing, rather than helpful.
Re: [php] Cannot modify header info
Quote:
Originally Posted by
Daevius
For me it never works till I use ob_start(); :S.
Then learn to code. Hell, in the error it says exactly where the output was started, all you have to do is go to that file and line and remove the output. As said, it's usually some blank space before or after your PHP tags.
I agree a 100% with DeathArt here, short time hacks are never a worthy solution.
Re: [php] Cannot modify header info
Well, I read it on php.net, and someone told me to use it on another forum. Anyways, I never used cookies ^^
Re: [php] Cannot modify header info
Quote:
Originally Posted by
Mario_Party
As Wh005h says you are right and wrong. Although I know you are trying to help, please learn the facts before telling people the wrong things, as learning several different things is only more confusing, rather than helpful.
LOOL, you must be reading my post in the wrong way, I said that the empty spaces weren't the problem in THIS case...