Why I get ERROR: Cannot modify header information?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Why I get ERROR: Cannot modify header information?

Post 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!!!
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Post 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!?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Post by yosuke_ »

thank you! but can you plesae check the script and help me?? i realy need hep with this one!!!
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

make sure you dont have any spaces before the <?php for the first problem
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Post 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!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Post by yosuke_ »

THANK YOU!!! You ware right! Everythin works now!! thank you!!
Post Reply