6 Replies - 735 Views - Last Post: 07 June 2008 - 02:36 PM Rate Topic: -----

#1 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

PHP header issue

Posted 07 June 2008 - 10:45 AM

I am using header to redirect to a different page and i get this error when the header line is crossed.
Warning: Cannot modify header information - headers already sent by (output started at /location/of/file/login.php:17) in /location/of/file/login.php on line 19



and here is the code area where it runs
if($result['password'] == $_POST['entered_password'])
	{
		echo 'login successful';
		//setcookie("user", $result['id'], time()+3600);
		header( 'Location: client/index.php' );
	}



Is This A Good Question/Topic? 0
  • +

Replies To: PHP header issue

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: PHP header issue

Posted 07 June 2008 - 10:50 AM

You can't print or echo anything prior to using the header function. This is because any time you use print or echo you are causing the response headers of the page to be sent to the user. Print and echo are actually writing to a stream so when you start writing it sends the headers of the page, making it too late to modify the headers using the header() function.

To solve this make sure you do not print ANYTHING prior to making a header() call. This means no printing or echoing any spaces, tabs, lines or anything which would cause the headers of the page to be sent.

So to fix your code, comment out the echo line and if you have no other echos previous to this code, it should work fine.

You can read up on this more at php.net for the header() function.

Hope this helps you out!

"At DIC we be header sending code ninjas... no I said 'heading'! Get your mind out of the gutter" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: PHP header issue

Posted 07 June 2008 - 10:58 AM

yup that makes sense, actually the echo there is just for my testing purposes I'm going to write a cookie their instead i don't think that would be a problem?
Thank you.
I appreciate it,
especially with the fast response.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: PHP header issue

Posted 07 June 2008 - 11:13 AM

I don't imagine writing to a cookie would cause a problem. :)
Was This Post Helpful? 0
  • +
  • -

#5 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: PHP header issue

Posted 07 June 2008 - 01:06 PM

Okay now I have an awkward problem, my mysql results returns only the first user, so only 1 user is able to login, and im not sure why, and also in my foreach loop i used $result appose to $value since when i used $value i got gibberish back, and the echo statment is for my checking purposes.
Any help would be greatly appreciated, I also check my sql statement with phpmyadmin and it returns all the users like it should.

$result = mysql_fetch_array($row);
$match = false;
foreach($result as $value)
{
echo $result['username'].'------'.$result['password'];
	if($result['username'] == $_POST['entered_name'])
	{
		if($result['password'] == $_POST['entered_password'])
		{
			$match = true;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: PHP header issue

Posted 07 June 2008 - 02:12 PM

if you're creating a login system, you should set the SQL to select 1 account where the username and the password are those typed by the user...
Was This Post Helpful? 0
  • +
  • -

#7 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 796
  • Joined: 25-February 08

Re: PHP header issue

Posted 07 June 2008 - 02:36 PM

Post all your code. Specifically where your sql statement is.

Also, check the PHP tutorials for how to do a basic authentication system. It's all in there.

This post has been edited by akozlik: 07 June 2008 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1