Jump to content

Warning: Cannot modify header information - headers already sent by


Recommended Posts

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

[code]<?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>[/code]

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

[code]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[/code]

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

[code]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[/code]

...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!
[!--quoteo(post=360866:date=Apr 2 2006, 03:11 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 2 2006, 03:11 PM) [snapback]360866[/snapback][/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) [snapback]360867[/snapback][/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!
[!--quoteo(post=360884:date=Apr 2 2006, 03:47 PM:name=newPHPer)--][div class=\'quotetop\']QUOTE(newPHPer @ Apr 2 2006, 03:47 PM) [snapback]360884[/snapback][/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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.