Author Topic: Headers already being sent  (Read 1731 times)

nate11985

  • Guest
Headers already being sent
« on: January 29, 2014, 02:51:31 am »
I uploaded my site to the schools server. I made sure everything was working great on my localhost before uplaoding. However, once I uploaded everything I have been recieving errors after registering and logging in. This is what the errors look like-

Warning: Cannot modify header information - headers already sent by (output started at /ebs/home/nate11985/domains/nate11985.aisites.com/public_html/mqa/index.php:7) in /ebs/home/nate11985/domains/nate11985.aisites.com/public_html/mqa/login-exec.php on line 32

How could I fix this issue?

I suspect it has something to do with the creation of cookies (since the errors are on the lines where I created cookies) and the last one has to do with using headers to relocate the user to a different page with php with this line of code:

header ('Location: main.php');

What can I do here to remedy this issue?

Also, after uploading the files, my CSS became a bit off. Nothing is really porportioned as I had it on Localhost, but that is easily fixed.

JennaSetticasi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 492
Re: Headers already being sent
« Reply #1 on: January 29, 2014, 06:30:24 am »
I would check your code to make sure that the call to header() is being made before any HTML output as that is usually the culprit for the error message you are describing. You can learn more here:  http://stackoverflow.com/questions/8028957/headers-already-sent-by-php. I would start by checking the line numbers indicated in the error.

Please let us know if you are able to find the code causing the error.
Jenna Setticasi
The Art Institute of Pittsburgh | Online Division
Associate Professor, Web Design and Interactive Media

nate11985

  • Guest
Re: Headers already being sent
« Reply #2 on: January 29, 2014, 04:43:05 pm »
Thanks Jenna.

I can see where the problem is. The biggest problem that I am facing is the redirection method that I am using. I'll try to explain, sorry if I confuse you...

In part of my code I have an if statement that looks to see if the button has been pressed. Once the button is pressed a few lines of code will be executed. At the last part of the code block, I have a redirection code which looks like this:

     header('Location: main.php');

Basically what this script is doing, is it is logging the user in by creating the sessions and cookies. Once the session and cookies are created the user is then redirected to the main.php page. The redirection code is placed in the middle of the page. I will upload the page that the code is on here (login-exec.php). The login-exec page is included in the index.php page, which I will upload as well.

In the login-exec.php page, the errors are on lines 28, 29, and 32. Lines 28 and 29 create the cookies and line 32 is where you will see the header code.
« Last Edit: January 29, 2014, 04:44:48 pm by nate11985 »

nate11985

  • Guest
Re: Headers already being sent
« Reply #3 on: January 29, 2014, 05:06:43 pm »
Got it! I had to create an additional page to set the cookies. I just got rid of the cookie code from the login-exec.php page and moved it to another page. The login-exec page now redirects to a page that creates the cookies. The page that cretes the cookies creates the cookies and then redirected the user to the main.php page.

To fix the calls to the header, I actually changed the code to look like this:

            $url='main.php';
       echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

Works great now! :)

JennaSetticasi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 492
Re: Headers already being sent
« Reply #4 on: January 30, 2014, 06:05:11 am »
That's interesting. Thanks for sharing!
Jenna Setticasi
The Art Institute of Pittsburgh | Online Division
Associate Professor, Web Design and Interactive Media