Page 1 of 1

PHP Problem - using header()

Posted: Wed May 11, 2005 4:40 am
by JoeCullen
Ok, I am trying to create a basic login system for my site. I have been testing and without using include files, the code works great. But with the includes I get:
Warning: Cannot modify header information - headers already sent by (output started at /home/joecul41/public_html/index.php:11) in /home/joecul41/public_html/inc_files/login.inc on line 24
This is my code:

Code: Select all

if (isset($_POSTї'Submit']))
{
	$username = $_POSTї'username'];
	$password = $_POSTї'password'];
	$result = mysql_query(&quote;Select * From users where username='$username'&quote;,$link);
	
	if(mysql_num_rows($result)>0)
	{
		$row = mysql_fetch_array($result, MYSQL_BOTH);
		if($password == $rowї&quote;password&quote;])
		{
		   if($rowї&quote;securitylevel&quote;] == &quote;1&quote;)
                   {	
			$_POSTї'username'] = &quote;username&quote;;
			$_POSTї'password'] = &quote;password&quote;;
                        $msg = &quote;Admin Login&quote;;
                        header(&quote;Location: admin.php&quote;);
                   }
                   else
                      {
                      $msg = &quote;Member Login&quote;;
                      }
		}
		else
		{
			$msg = &quote;Password incorrect&quote;;
		}
	}
	else
	{
		$msg = &quote;Username incorrect&quote;;
    }
}
?>
I know the connection string and everything is correct as the code works fine without the header command. Is there any way to link to another page or open a file without using header()?

As you can probaly tell im new to coding so please any simple solutions would be appreciated.

Thanks in advance.

Posted: Wed May 11, 2005 4:48 am
by hongco
here is a tutorial:
viewtopic.php?t=1157
:)

Posted: Wed May 11, 2005 5:03 am
by Revan
You probably have already sent data on the page the header is included in.

Posted: Wed May 11, 2005 5:21 am
by JoeCullen
ahh yes i see what im doing wrong now - thanks for that link.

instead of using the header() is there a simple we to redirect?

I noticed on that link there were Java redirects. Does PHP not have a redirect function?

Posted: Wed May 11, 2005 5:27 am
by malcolmboston
JoeCullen wrote:Does PHP not have a redirect function?
Yeah, header() function does it, however, as you have noticed when headers have already been sent you cannot use this to redirect.

Go through your source code and re-organise it

Posted: Wed May 11, 2005 6:56 am
by John Cartwright
Alternatively, you could use javascript's location.

Posted: Wed May 11, 2005 9:20 am
by Chris Corbyn
Meta tags.... http-equiv="refresh"....

*yawn*

Posted: Wed May 11, 2005 10:27 am
by JoeCullen
I think the easiest work around is the JavaScript Location command. Its probaly not the neatest but does the job :)

Thanks once again for all your help and ideas.