PHP Header Problem
Ok, I have made a CMS for my site/others but I am having some troubles with the login. I am trying to make it store a cookie - that's my problem. Yes, something as small as that is giving me hell. Here is the script.
<p>
<link href="../layout.css" type="text/css" rel="stylesheet" /></p>
<div id="wrap">
<div id="header">
<h1>Flaming_Figures Administrative Login</h1>
</div>
<div id="nav">
<ul>
<li><a href="../index.php">Home</a></li>
<li><a href="index.php">Login</a></li>
</ul>
</div>
<div id="main">
<?php
$username = md5($_REQUEST['username']);
$password = md5($_REQUEST['password']);
if (isset($_REQUEST['submit'])){
if ($username == md5("guest") && $password == md5("password")){
setcookie('admin','TRUE',time()+3600);
echo "Main content goes here";
}
else
{
echo "Incorrect Credentials";
}
}
else
{
echo "
<form action='$PHP_SELF' method='post'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='submit' name='submit' />
</form>";
}
?>;
</div>
<div id="sidebar">~::Insert your ads here::~</div>
<div id="footer">
<p>Footer</p>
</div>
</div>
<div id="rightcol"> </div>
<div id="leftcol"> </div>
When I do that it send out an error saying that the headers cannot be modified because they are already outputting header info on line 13. Any help appreciated and yes, I know, this is a gay small question but I do not want to go insane simply because it IS such a small thing. (COOKIES for gods sake!!! I can't believe my old files got wiped off my old computer… backup flaming BACKUP!!)
NOTE If you notice many HTML mess ups, that is because I am using 110mb and they like to screw with your script when you look at it, which is why I copy and paste every edit.
hmm, you need to output cookies before anything else gets outputted. just move your setcookie() to before you output html and it should work. this tripped me up too, and some more advice about cookies: just keep in mind also that cookies are unreadable immediately after you set them, because you receive the cookie from the client before you send the new one. that confused me for a while. :P
Ok, I think I understand where you're getting at, but, well, I don't… I can't exactly put the PHP before all HTML, it needs to be in the proper DIV… Perhaps if I make it a function but even then I am doubtful… I will try making it a function but if anyone else has any ideas they are greatly accepted.
EDIT
Ok, so I redid my script putting the PHP at the very top of the page, made that a function and activated it in the main DIV.
New Script
<?php
function loginAndContent()
{
$username = md5($_REQUEST['username']);
$password = md5($_REQUEST['password']);
if (isset($_REQUEST['submit'])){
if ($username == md5("guest") && $password == md5("password")){
setcookie('admin','TRUE',time()+3600);
echo "Main content goes here";
}
else
{
echo "Incorrect Credentials";
}
}
else
{
echo "
<form action='$PHP_SELF' method='post'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='submit' name='submit' />
</form>";
}
}
?>
<p>
<link href="../layout.css" type="text/css" rel="stylesheet" /></p>
<div id="wrap">
<div id="header">
<h1>Flaming_Figures Administrative Login</h1>
</div>
<div id="nav">
<ul>
<li><a href="../index.php">Home</a></li>
<li><a href="index.php">Login</a></li>
</ul>
</div>
<div id="main">
<?php
loginAndContent();
?>
</div>
<div id="sidebar">~::Insert your ads here::~</div>
<div id="footer">
<p>Footer</p>
</div>
</div>
<div id="rightcol"> </div>
<div id="leftcol"> </div>
But it still gives me the same error. I know I would have to do it at the very beginning, but how do I go about that if making it a function doesn't work and it needs to be in the main div? Thanks BTW
If you want to do it the easy way just add the function ob_start() at the beginning of your code and ob_end_flush at the end of your code.
What it do, is that it wait till the end of the script to output the HTML, so all the header are send before.
Edit: You shouldn't use a PHP version under 5 or 4. PHP 4 was already announce.
Using output buffers is definitely the easy solution, though I never use that as I consider it less clean. Instead I prefer to run the cookies things as early as possible. Then if I need to output related info later I set a variable, say $comment = "your cookies has been set". Then later on if (isset($comment)) { echo $comment; }
Heh. I guess it could be argued my solution is messier.
No, it's not. While Arto's solution works really well lifts pirate hat gently yours avoids the problem completely. I'd say play it safe and go for the "avoiding-the-whole-situation-with-three-easy-lines-of-code" way.
But if this code isn't meant for long-time use and you are too tired/lazy to change it, again, Arto's solution does the trick.
I added in artos solution, and now the page is white… Hmm… And what it is is an authentication cookie, so I can't exactly say, "Ok, you already have the cookie even before typing any username or password." That would just be stupid. I have put it like, first line of being authenticated
if ($username == md5("guest") && $password == md5("password")){ setcookie('admin','TRUE',time()+3600); echo "Main content goes here"; * And that shot out the header error.
Flaming_figures wrote: I added in artos solution, and now the page is white… Hmm… And what it is is an authentication cookie, so I can't exactly say, "Ok, you already have the cookie even before typing any username or password." That would just be stupid. I have put it like, first line of being authenticated
if ($username == md5("guest") && $password == md5("password")){ setcookie('admin','TRUE',time()+3600); echo "Main content goes here"; * And that shot out the header error.
Do you have the function ob_end_flush at the end ?