Page 1 of 1

Why I get ERROR: Cannot modify header information?

Posted: Sun Apr 18, 2004 5:22 am
by yosuke_
Well, I creted a simple login script, but I Get error: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\login.php: 18) in c:\inetpub\wwwroot\login.php on line 21 The code:

Code: Select all

<?
$username = $_POST["txtusername"];
$password = $_POST["txtpassword"];
if ($username=="" || $password=="")
{
echo "<br><br><br><br><br><center><table border='0' cellpadding='2' bgcolor='#F3F3F3'><tr><td><font face='verdana' size='1'>Please enter information in all text fields</td></tr>";
}
else
	{
	error_reporting(E_ALL); 
	$db = mysql_connect('localhost','root','q6ze32y8k1') or die(mysql_error()); 
	mysql_select_db("database") or die(mysql_error()); 
	$result = mysql_query("SELECT * FROM users"); 
	while($results = mysql_fetch_array($result))
	{
	if ($results['username'] == $username && base64_decode($results['password']) == $password) 
		{
		echo "You are loged in successfully<br>";
				if(isset($_POST['Cookie'])) {
				$set_cookie = $_POST['Cookie']; 
				setcookie("haveacookie",$password . "<explode_this>" . $username,time() + 360000);
				}
				else
				{ 
				$set_cookie = null;
			}
}
}
}
?>
please help!! I need to set cookie with username and password, if user login is successfull!! Please help me!!!

Posted: Sun Apr 18, 2004 5:43 am
by yosuke_
And another question about login form!! When I renter invalid username or password it shows blank page, but If I will create login page like this:

Code: Select all

<?php
  if ($results['username'] == $username && base64_decode($results['password']) == $password) 
      { 
      echo "You are loged in successfully<br>";
      }
      else
      {
      echo "Invalid username or password";
      }
?>
If username will be invalid I will get about 20 errors of invalid username, because i have 20 usernames in database and if I will add break; It will stop looping after first invalid username!!! How can I make it loop thro all records and after then Just say once that it is invalid username!?

Posted: Sun Apr 18, 2004 5:49 am
by malcolmboston
ok, i havent read your post

but basically you cannot send any output to the browser BEFORE you use teh header function, simple as that, no print statements no whitespace nothing

a bit of reordering in your script will sort it out

Posted: Sun Apr 18, 2004 6:41 am
by yosuke_
thank you! but can you plesae check the script and help me?? i realy need hep with this one!!!

Posted: Sun Apr 18, 2004 9:29 am
by Deemo
make sure you dont have any spaces before the <?php for the first problem

Posted: Sun Apr 18, 2004 9:51 am
by yosuke_
I dont have any spaces there!!! Is there something wrong with cookie? I remember that I read someware that cookie hase to be set in the code beggining??? have can I modify this code, si it will give user a cookie if login will be successfull?? thank you!

Posted: Sun Apr 18, 2004 9:56 am
by kettle_drum

Code: Select all

echo "You are loged in successfully<br>"; 
            if(isset($_POST['Cookie'])) { 
            $set_cookie = $_POST['Cookie']; 
            setcookie("haveacookie",$password . "<explode_this>" . $username,time() + 360000); 
            }
You cannot echo "You are loged in successfully<br>" before the cookie is set. Move this line to the bottom of the if statements.

Posted: Sun Apr 18, 2004 10:56 am
by yosuke_
THANK YOU!!! You ware right! Everythin works now!! thank you!!