User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,015 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,005 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 5777 | Replies: 7 | Solved
Reply
Join Date: May 2007
Posts: 59
Reputation: dudegio is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dudegio dudegio is offline Offline
Junior Poster in Training

Help PHP Warning: Cannot modify header information - headers already sent by (output .....

  #1  
Sep 4th, 2007
Hello please help me troubleshooting this. i got this error in my login.. pleaaaaase. below is the error that prompts me when i login:


PHP Warning: Cannot modify header information - headers already sent by (output started at D:\HostingSpaces\spindev\spindev.spinweb.ph\wwwroot\YPS\login\login.php:9) in D:\HostingSpaces\spindev\spindev.spinweb.ph\wwwroot\YPS\login\login.php on line 71 PHP Warning: Cannot modify header information - headers already sent by (output started at D:\HostingSpaces\spindev\spindev.spinweb.ph\wwwroot\YPS\login\login.php:9) in D:\HostingSpaces\spindev\spindev.spinweb.ph\wwwroot\YPS\login\login.php on line 72 PHP Warning: Cannot modify header information - headers already sent by (output started at D:\HostingSpaces\spindev\spindev.spinweb.ph\wwwroot\YPS\login\login.php:9) in D:\HostingSpaces\spindev\spindev.spinweb.ph\wwwroot\YPS\login\login.php on line 75


Here is my code: please tell me what's going wrong. thanks.

<?php
// Connects to your Database
mysql_connect("spindev.spinweb.ph", "spindev_ypsuser", "yps123") or die(mysql_error());
mysql_select_db("spindev_ypsnew") or die(mysql_error());
//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}
//otherwise they are shown the admin area
else
{
echo "Admin Area<p>";
echo "Your Content<p>";
echo "<a href=logout.php>Logout</a>";
}
}
}
else
//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}
?>


AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: Palmerston North, New Zealand
Posts: 16
Reputation: WhiteLeo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
WhiteLeo's Avatar
WhiteLeo WhiteLeo is offline Offline
Newbie Poster

Re: PHP Warning: Cannot modify header information - headers already sent by (output .

  #2  
Sep 4th, 2007
From first glance it looks like you've placed echo above the header location script.

Headers must be parsed before any HTML, and for some reason (Unknown by me, but I'm sure others can answer why) certain hosts don't like it when you place any HTML above header tags.

I would stick the HTML below the php code, specifically below the Header("Location: login.php") script.

Eg:
  1. <?php
  2. // Connects to your Database
  3. mysql_connect("spindev.spinweb.ph", "spindev_ypsuser", "yps123") or die(mysql_error());
  4. mysql_select_db("spindev_ypsnew") or die(mysql_error());
  5. //checks cookies to make sure they are logged in
  6. if(isset($_COOKIE['ID_my_site']))
  7. {
  8. $username = $_COOKIE['ID_my_site'];
  9. $pass = $_COOKIE['Key_my_site'];
  10. $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
  11. while($info = mysql_fetch_array( $check ))
  12. {
  13. //if the cookie has the wrong password, they are taken to the login page
  14. if ($pass != $info['password'])
  15. {
  16. header("Location: login.php");
  17. }
  18. //otherwise they are shown the admin area
  19. else
  20. {
  21. //echo "Admin Area<p>";
  22. //echo "Your Content<p>";
  23. //echo "<a href=logout.php>Logout</a>";
  24. }
  25. }
  26. }
  27. else
  28. //if the cookie does not exist, they are taken to the login screen
  29. {
  30. header("Location: login.php");
  31. }
  32. ?>
  33. <h1>Admin Area</h1>
  34. <p>Your content</p>
  35. <p><a href="logout.php">Logout</a>

I could be completely off the mark, since it is late. I'd suggest tweaking the order of your conditions so that if an error occurs the user is directed away from the page, otherwise the html at the end loads on a successful log in.
Reply With Quote  
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: PHP Warning: Cannot modify header information - headers already sent by (output .

  #3  
Sep 4th, 2007
try placing this at the top of your page:
<? ob_start(); ?>

then at the bottom of the page place this line of code:

<? ob_flush(); ?>
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote  
Join Date: May 2007
Posts: 59
Reputation: dudegio is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dudegio dudegio is offline Offline
Junior Poster in Training

Re: PHP Warning: Cannot modify header information - headers already sent by (output .....

  #4  
Sep 4th, 2007
Thanks to both of you. I have tried your solution JeniF and the login script works. Thanks a lot. I will try to implement your solutions as well WhiteLeo so that i do have lot of references if it works. Thank you very much again.
Last edited by dudegio : Sep 4th, 2007 at 10:27 pm.
Reply With Quote  
Join Date: Sep 2007
Location: India
Posts: 8
Reputation: sujithfem is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sujithfem sujithfem is offline Offline
Newbie Poster

Re: PHP Warning: Cannot modify header information - headers already sent by (output .

  #5  
Sep 11th, 2007
Really Thank you for your Valuable info.....
Reply With Quote  
Join Date: Feb 2008
Posts: 1
Reputation: TaiShar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TaiShar TaiShar is offline Offline
Newbie Poster

Re: PHP Warning: Cannot modify header information - headers already sent by (output .

  #6  
Feb 19th, 2008
Originally Posted by JeniF View Post
try placing this at the top of your page:
<? ob_start(); ?>

then at the bottom of the page place this line of code:

<? ob_flush(); ?>

_________________________________________________
Thank you so much JeniF. I have been receiving a similar error for days now, and have been unable to complete a class assignment. Popped your suggestion in, and it now works. My elation is directly correlated to my previous frustration, and I just wanted to take the time to say thanks. Have a good one.

(Incidentally, if you get the time, what exactly is the recommended code doing?)
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: PHP Warning: Cannot modify header information - headers already sent by (output .....

  #7  
Feb 19th, 2008
(Incidentally, if you get the time, what exactly is the recommended code doing?)

ob_start() . Its turning on the output buffer. So any output is kept in the buffer. And ob_flush() is to flush the buffer
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: May 2008
Posts: 1
Reputation: xjessie007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xjessie007 xjessie007 is offline Offline
Newbie Poster

Re: PHP Warning: Cannot modify header information - headers already sent by (output .....

  #8  
May 19th, 2008
I had a problem with the "headers already sent" too at my http://www.maxi-pedia.com website. In my case, it was caused by a blank line at the beginning of one *.inc file. PHP seems to have problems with extra spaces here and there.

Check all your *.inc files to make sure you do not have closing ?> in any of them. Closing ?> is not needed in your *.inc files. Check all your *.php files to make sure you do not have blank lines at the beginning or at the end.

If you are working in a CMS, then it may be caused by some module, disable your modules one by one to find out which one causes this.

This can also be caused by UTF-8. If you have your website coded in ASCII and are saving your php files as UTF-8, it can cause this message. If your website and DB are UTF-8, you should be ok with saving php files as UTF-8 though.

Btw, this error message is related to output_buffering on/off in your php.ini. If you have output_buffering set to some cache, the server will allow to send headers with delay (or to modify them shortly after they are sent), and this error will not be tripped. But if you set output_buffering to 0 or not at all, then headers can be sent at only one moment, and if you have some bad code, it will trip this error message.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC