7 Replies - 1003 Views - Last Post: 26 September 2011 - 07:03 AM Rate Topic: -----

#1 Samlock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-November 09

PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 06:35 AM

Hi all,

First post here, so go easy on me if I'm asking a question which has been asked a million times before. I have researched this issue, and the general solution is "make sure nothing is being sent before the header" which, as far as I can tell - it isn't!

I'm getting the error "Cannot modify header information - headers already sent" on my project I'm working on. "Fine" I thought - "I'll build a REALLY simple test script to see if it's something I'm getting wrong"... The code for the login page is below:

<?php header('location:wrong.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	$un = 'Sam';
	$pw = 'Sam';
	$un2 = 'Sam2';
	$pw2 = 'Sam2';
	$username = $_POST['username'];
	$password = $_POST['password'];
	if (($username == $un) && ($password == $pw)){
		$_SESSION['username'] = $username;
		echo "Hello ".$username;
		echo "<br /><a href='right.php'>Test</a>";
	}
	elseif (($username == $un2) && ($password == $pw2)){
		$_SESSION['username'] = $username;
		echo "Hello ".$username;
		echo "<br /><a href='right.php'>Test</a>";
	}
	else
	{
		header('location:wrong.php');
	}
}
?>
<html>
<form id='login' method='post' action='index.php'>
<input type='text' name='username'>
<input type='text' name='password'>
<input type='submit' value='Submit'>
</form>


It's quick and simple, I know nothing is being cleansed and it's an awful layout but I'm learning and it's only a test! :)

When running locally in XAMPP, this works absolutely fine and I can log in/check stuff and it all works fine. On my web hosting with one.com however, it doesn't work. If you want to have a look at the result I'm getting, check it at http://www.moozik.me...ession-testing/

I'm at a complete loss with this one, I moved the line "header('location:wrong.php');" to line 1 (tried on line 2, leaving just "<?php" on line 1 too) to see if I was missing something, but it still won't work.

I had also tried it without the first "header" redirect on lines 1 or 2, just on line 21 where it is called if the two If statements return false.

Any help or advice would be very much appreciated, and I do hope I'm not breaking any forum rules by not searching or something, believe me I've searched!

Thanks :)

.:EDIT:.

Forgot to add, I've also had "session_start();" at the top of the page (line 1 and 2 to try it) which again works fine locally, but when on one.com hosting, it won't take it. According to phpinfo(), Auto session start is off...? *confused smily which I can't find*

This post has been edited by Samlock: 26 September 2011 - 06:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP - Cannot modify header information (trying to set on line 1)

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 06:40 AM

Hi,

Are you sure there are no blank lines at the top of your file before you open your PHP tags?
Was This Post Helpful? 0
  • +
  • -

#3 Samlock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-November 09

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 06:43 AM

Absolutely certain, that was my first thought and also why I moved the header onto line 1, in case it didn't like it being on line 2 after <?php...!
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 06:48 AM

Can you please try this:
<?php
header('Location: http://google.com/');
die();


.. in a script file and see what happens.
Was This Post Helpful? 0
  • +
  • -

#5 Samlock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-November 09

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 06:52 AM

View PostRudiVisser, on 26 September 2011 - 07:48 AM, said:

Can you please try this:
<?php
header('Location: http://google.com/');
die();


.. in a script file and see what happens.


Done :)

Locally: redirects me to Google.

On one.com: http://www.moozik.me.../rudivisser.php

Same error...!

.:EDIT:.

Just had a thought... It wouldn't be caused by being behind a proxy server, would it? I'm at work at the moment, through a proxy.

This post has been edited by Samlock: 26 September 2011 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 06:58 AM

Nope that's all server side, nothing you browse it with could change the output.

I think your issue is that you're creating a file in UTF-8 mode (with the BOM).. What editor are you using when you create this file?

If it's Notepad++, change the file encoding to be UTF-8 without BOM or an ISO, and then save / re-upload the file.
Was This Post Helpful? 2
  • +
  • -

#7 Samlock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-November 09

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 07:02 AM

Hmm.. Hadn't thought of that! I'm using Notepad++, but I did have an issue in the past with trying to get the Compare plug-in to work, something to do with it being built for ANSI but my installed build being UTF-8 or something along those lines. I'll try it in plain old Notepad and see if that makes a difference.

...

Success! It works when I create it in Notepad, and on the bottom bar of Notepad++ it says UTF-8. If I change the preferences to use ANSI instead of UTF-8, and recreate all the files it should work I guess?

If that's it, THANK YOU! It's been bugging me for ages!

.:EDIT:.

Sorry in my eagerness to test that, I didn't read your post fully. Recreated it with the settings you suggested changed and it works perfectly.

Thank you so much :)

This post has been edited by Samlock: 26 September 2011 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,175
  • Joined: 08-June 10

Re: PHP - Cannot modify header information (trying to set on line 1)

Posted 26 September 2011 - 07:03 AM

View PostRudiVisser, on 26 September 2011 - 03:58 PM, said:

I think your issue is that you're creating a file in UTF-8 mode (with the BOM)..

and if you locally have automatic output buffering switched on (which is a setting in php.ini), you simply do not encounter the problem. (though it is better to have it off and have the problem fixed)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1