PHP Problem - using header()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JoeCullen
Forum Newbie
Posts: 3
Joined: Wed May 11, 2005 3:51 am
Location: UK

PHP Problem - using header()

Post 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.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

here is a tutorial:
viewtopic.php?t=1157
:)
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post by Revan »

You probably have already sent data on the page the header is included in.
JoeCullen
Forum Newbie
Posts: 3
Joined: Wed May 11, 2005 3:51 am
Location: UK

Post 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?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Alternatively, you could use javascript's location.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Meta tags.... http-equiv="refresh"....

*yawn*
JoeCullen
Forum Newbie
Posts: 3
Joined: Wed May 11, 2005 3:51 am
Location: UK

Post 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.
Post Reply