Annoying headers already sent error - PHP

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 642 Views - Last Post: 30 January 2012 - 09:18 PM Rate Topic: -----

Topic Sponsor:

#1 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Annoying headers already sent error - PHP

Posted 29 January 2012 - 01:03 AM

I get the followong error:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/08/8207708/html/connect.php:11) in /home/content/08/8207708/html/check_login.php on line 17

When i try to log into my site online.

<?php
include('connect.php');
$tbl_name="users"; 
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_start();
session_register("myusername");
session_register("mypassword");
header("location:index2.php");}
else {
echo "Wrong Username or Password";
}
?>




There are no spaces at the beginning or at the end. and i cleared all white space between the lines to make sure. I can put the session_start at the beginning but.. then how can i check the password is correct or not if it comes before the other stuff. Bit confused on this as it works on Xampp but when i try online i get the error.

Is This A Good Question/Topic? 0
  • +

Replies To: Annoying headers already sent error - PHP

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,426
  • Joined: 08-June 10

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 01:32 AM

1) the output PHP complains about is in connect.php line #11 (not sure what it is, since there is no listing)

2) XAMPP may have output buffering enabled
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3050
  • View blog
  • Posts: 22,959
  • Joined: 10-May 07

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 01:38 AM

Quote

if($count==1){
session_start();
session_register("myusername");
session_register("mypassword");
header("location:index2.php");}
else {
echo "Wrong Username or Password";
}

I had a difficult time finding the end bracket after you load index2.php. You should really start using proper indenting & clean code now, before it becomes a problem. You don't want to waste time looking for phantom issues because of sloppy code.

if($count==1){
  session_start();
  session_register("myusername");
  session_register("mypassword");
  header("location:index2.php");
}
else {
  echo "Wrong Username or Password";
}

Was This Post Helpful? 0
  • +
  • -

#4 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 01:39 AM

i added ob_flush() to the beginning of the code and ob_end_flush() to the end. This allowed me to log in, however i have this error now at the top of page.


Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/08/8207708/html/connect.php:11) in /home/content/08/8207708/html/auth.php on line 3

<?php
$host="host"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>




<?php
ob_start();
include('connect.php');
$tbl_name="users"; 
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_start();
session_register("myusername");
session_register("mypassword");
header("location:index2.php");}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>


Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,426
  • Joined: 08-June 10

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 01:49 AM

question: what is connect.php lines #10-#12? (though you have 9 lines given, PHP complains about something in line #11)
Was This Post Helpful? 0
  • +
  • -

#6 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 02:08 AM

i think it must have been some //notes i took out, i cant think why else
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • No Sugar Coding Here!
  • member icon

Reputation: 4683
  • View blog
  • Posts: 20,361
  • Joined: 23-August 08

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 05:54 AM

Perhaps some whitespace at the end of the file?
Was This Post Helpful? 0
  • +
  • -

#8 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 05:01 PM

i removed all whitespace from beginnig and end.Also made sure is UTF-8 without BOM. Im using notepad ++.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • No Sugar Coding Here!
  • member icon

Reputation: 4683
  • View blog
  • Posts: 20,361
  • Joined: 23-August 08

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 05:18 PM

Maybe you should zip up all your code and attach it to a post.
Was This Post Helpful? 0
  • +
  • -

#10 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 07:43 PM

ok i got it working. In my index2 file i have auth.php at the top which check the user is logged in. i removed ob_end_flush() from the bottom of my above code and put it in this file instead. So thanks for all your help and suggestions. :bananaman:

The only problem now, is none of my images or icons appear. For example i have many icons for links stored in a folder 'icons'.

Also i have javascript files in a folder called 'js'

Anything in a folder isnt referenced properly. If i take an image out and drop it in the main folder, or put the javascipt files in the main folder it works. But having everything in one place isnt how anyone wants it.

i dont understand as it all references fine in xampp. i have tried relative and absolute references but anything in a folder doesnt work. Any one got any suggestions here?


<img src="icons/addstudent.png"/>
<img src="www.mysite.com/icons/addstudent.png"/>
<img src="http://www.mysite.com/icons/addstudent.png"/>

but only this works (when i remove image from icons and put in main folder)

<img src="addstudent.png"/>



Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3050
  • View blog
  • Posts: 22,959
  • Joined: 10-May 07

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 07:49 PM

View Postsquibby, on 29 January 2012 - 09:43 PM, said:

i dont understand as it all references fine in xampp.

<img src="icons/addstudent.png"/>


If icons is not a sub-directory of the current directory, this link fails.

View Postsquibby, on 29 January 2012 - 09:43 PM, said:

<img src="www.mysite.com/icons/addstudent.png"/>


This is not a valid url. You must first specify the protocol.

View Postsquibby, on 29 January 2012 - 09:43 PM, said:

<img src="http://www.mysite.com/icons/addstudent.png"/>


This is valid. Hover it's only valid. Does it work if you plug it into a browser? If not, then it's not correct. Valid, but not correct.

View Postsquibby, on 29 January 2012 - 09:43 PM, said:

i dont understand as it all references fine in xampp.

<img src="addstudent.png"/>


If this works, it's because you are currently in the directory where the file is located.
Was This Post Helpful? 0
  • +
  • -

#12 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 11:32 PM

<img src="icons/addstudent.png"/>



icons IS a sub directory of the main root directory. i made no changes from when using in xampp to when i went online. i just moved it all across using filzilla.

Another strange thing, is that on some of the pages there are placholders for where the image icons should be, and on other pages not at all.
Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3050
  • View blog
  • Posts: 22,959
  • Joined: 10-May 07

Re: Annoying headers already sent error - PHP

Posted 29 January 2012 - 11:43 PM

View Postsquibby, on 30 January 2012 - 01:32 AM, said:

<img src="icons/addstudent.png"/>



icons IS a sub directory of the main root directory.

So when it works, right click the image. What is the full url for the image? Is it the same as http://www.mysite.co.../addstudent.png, from your img tag?
Was This Post Helpful? 0
  • +
  • -

#14 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 21-January 12

Re: Annoying headers already sent error - PHP

Posted 30 January 2012 - 12:30 AM

the images only show if i change code to like this

<img src="addstudent.png"/>



and put the files directly in the root directory. When i right click i get

http://www.mysite.com/addstudent.png

which i expected. Now if i put the image in icons folder and do this

<img src="icons/addstudent.png"/>




or

<img src="http://www.mysite.com/icons/addstudent.png"/>



it doesnt show up. why i have no idea. It makes no sense. im tempted to just put all the images in the root folder and be done with it. but it should work, this seems such a basic thing to get working.
Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3050
  • View blog
  • Posts: 22,959
  • Joined: 10-May 07

Re: Annoying headers already sent error - PHP

Posted 30 January 2012 - 12:35 AM

Forgive me if I'm over looking it, but is this a Windows or a Linux/Unix webserver? Does the sub directory have the correct permissions for the apache service (user) to enter?

Overall, you can also read the apache logs to see why it reports the images are not available.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2