Jump to content


Photo

Warning: Cannot modify header information - headers already sent by


  • Please log in to reply
6 replies to this topic

#1 iamali

iamali
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 April 2006 - 02:08 PM

Hi,
I have a problem with a login code I've written:

<?php
session_start();
$user = $_POST['username'];
$pass = $_POST['password'];
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP Photograph Gallery</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>

<?php 
include("links_private.inc");
?>

<table class="page" align="center" cellpadding="15" cellspacing="0">
<tr><td class="header"><h1>PHP Photograph Gallery</h1><br />Login Successful</td></tr>
<tr><td>

<?php
if ((!$_POST['username']) || (!$_POST['password'])) {
    header("Location:login.htm");
    exit;
}

include("dbconnect.inc");

$sql = "SELECT f_name, l_name from tbl_users where username = '$_POST[username]' AND password = password('$_POST[password]')"
    or die(mysql_error());
$result = mysql_query($sql, $conn)
    or die(mysql_error());

$num = mysql_num_rows($result); 

if ($num == 1) {
    $f_name = mysql_result($result,0,'f_name');
    $l_name = mysql_result($result,0,'l_name');
} else {
    header("Location:login.htm");
    exit;
}

echo "<p class=\"centre\">Welcome $f_name $l_name!</p>"; 
?>
    
<p class="centre"><a href="admin.php">Continue to Gallery Administration</a></p>

</td></tr>
</table>

</body>
</html>

whenever I try to log in with the username and password field as empty it gives me this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\wwwroot\galleryuploader\auth_login.php:18) in C:\Inetpub\wwwroot\galleryuploader\auth_login.php on line 28

and when I try and log in with incorrect details in the username and password field it gives me this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\wwwroot\galleryuploader\auth_login.php:18) in C:\Inetpub\wwwroot\galleryuploader\auth_login.php on line 45

...when i was hoping it would use the 'header("Location:login.htm");' to send the user back to the login page.

Any ideas why it's doing this anyone?
Thanks!


#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 02:11 PM

hi

there are several pinned topics entitled things such as 'Read here before posting' and 'Do you have header/session problems'. You really should read those first.

Cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 02 April 2006 - 02:12 PM

you have some stuff bering printed to the brower window befroe you call header. you need to put header before any output, i dont see why you can not do that in your code. just try to move it above the html

#4 Desdinova

Desdinova
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 02 April 2006 - 02:18 PM

or put a ob_start(); right at the beginning of the page, and a ob_end_flush(); at the very end. else, what emerhka (or something, can't remember sorry ;) ) said.

#5 iamali

iamali
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 April 2006 - 02:31 PM

[!--quoteo(post=360866:date=Apr 2 2006, 03:11 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 2 2006, 03:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
hi

there are several pinned topics entitled things such as 'Read here before posting' and 'Do you have header/session problems'. You really should read those first.

Cheers
Mark
[/quote]

Sorry I couldn't find them, there was only:

Pinned: Have you been helped here?
Pinned: Simple Error Handling/Form Validation Logic
Pinned: MYSQL_SOMETHING - INVALID RESOURCE
Pinned: DO YOU HAVE PHP SESSION PROBLEMS?

at the top of the forum. :-S

[!--quoteo(post=360867:date=Apr 2 2006, 03:12 PM:name=emehrkay)--][div class=\'quotetop\']QUOTE(emehrkay @ Apr 2 2006, 03:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
you have some stuff bering printed to the brower window befroe you call header. you need to put header before any output, i dont see why you can not do that in your code. just try to move it above the html
[/quote]

oh i see, so you can't have any HTML or anything before headers? ok I've moved it around and it works now :-) thanks!

#6 kwstephenchan

kwstephenchan
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 02 April 2006 - 02:47 PM

Desdinova is right. Put ob_start() at the very first line of the page and ob_end_flush() at the end. That should solve your problem.

#7 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 05:23 PM

[!--quoteo(post=360884:date=Apr 2 2006, 03:47 PM:name=newPHPer)--][div class=\'quotetop\']QUOTE(newPHPer @ Apr 2 2006, 03:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Desdinova is right. Put ob_start() at the very first line of the page and ob_end_flush() at the end. That should solve your problem.
[/quote]

whilst ob_start, etc will work, i kinda sometimes feel that it should be used as a last resort if you HAVE to resend the headers. otherwise, it's just a form of masking a problem or something that shouldn't be there.

[a href=\"http://www.phpfreaks.com/forums/index.php?showforum=12\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showforum=12[/a]
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users