Jump to content

header error


182x

Recommended Posts

Hey guys, I get the following error message when using the code below:

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\a\processLogin.php:10) in C:\xampp\htdocs\a\processLogin.php on line 48

 

I was just wondering how to fix it? Thanks.

 

line 48:

case 1: header("location:admin.html"); break;

<?php 
session_start(); 
include ('db.php');
$li = db('');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Login</title>
<style type="text/css">
<!--
.style1 {
font-size: 29px;
font-family:Helvetica, sans-serif;
}
-->
</style>
</head>
<body bgcolor="#CCCCCC">
<?php

$u = $_POST['u'];
$p = $_POST['p'];

$s = "SELECT * FROM `u` WHERE `u` ='$u'";
$r = mysql_query($s,li) or die(mysql_error());

if(mysql_num_rows($r) > 0)
{
$s2 = "SELECT * FROM `u` WHERE `u` ='$u' AND `p` ='$p'";

$r2 = mysql_query($s2,li) or die(mysql_error());

if(mysql_num_rows($r2) > 0){
	$r = mysql_fetch_assoc($r2);

	$l = $r[al];

switch($l)
{

	case 1: header("location:test.html"); break;
	case 2: header("location:test1.html"); break;
	exit();
}


}

else
{
	echo '<p align="center"><span class="style1">incorrect.</span></p>';
	echo '<p align="center"><a href="index.php" class="style1">Back</a></p>';
}


}

else {

echo '<p align="center"><span class="style1">error.</span></p>';
echo '<p align="center"><a href="index.php" class="style1">Back</a></p>';
}
?>
</body>
</html>

Link to comment
Share on other sites

still I can't figure out how to make it work and keep the HTML.

 

You can't> You need to fix your logic so that the html is only outputted after any calls to header. You don't need any html prior to a redirect anyway... makes little sense.

 

One hack would be to wrap all your code in...

 

<?php

  ob_start();
  // code here.
  ob_end_flush();

?>

 

But as I said... that is a hack.

Link to comment
Share on other sites

Move all PHP processing before the HTML, example:

<?php
session_start();

include ('db.php');

$li = db('');

$u = $_POST['u'];
$p = $_POST['p'];

$s = "SELECT * FROM `u` WHERE `u` ='$u'";
$r = mysql_query($s, $li) or die(mysql_error());

if(mysql_num_rows($r) > 0)
{
$s2 = "SELECT * FROM `u` WHERE `u` ='$u' AND `p` ='$p'";

$r2 = mysql_query($s2, $li) or die(mysql_error());

if(mysql_num_rows($r2) > 0)
    {
	$r = mysql_fetch_assoc($r2);
	$l = $r['al'];

    	switch($l)
    	{
            case 1:
                header("location:test.html");
            break;
    		case 2:
                header("location:test1.html");
            break;
    	}
    }
else
{
        $msg  = '<p align="center"><span class="style1">incorrect.</span></p>';
        $msg .= '<p align="center"><a href="index.php" class="style1">Back</a></p>';
}
}
else
{
    $msg  = '<p align="center"><span class="style1">error.</span></p>';
$msg .= '<p align="center"><a href="index.php" class="style1">Back</a></p>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Login</title>
<style type="text/css">
<!--
.style1 {
font-size: 29px;
font-family:Helvetica, sans-serif;
}
-->
</style>
</head>
<body bgcolor="#CCCCCC">
  <?php echo $msg; ?>
</body>
</html>

 

Or another method is to change these lines:

case 1: header("location:test.html"); break;
	case 2: header("location:test1.html"); break;

To this:

case 1: redirect_to("test.html"); break;
	case 2: redirect_to("test1.html"); break;

 

Then add this code:

function redirect_to($location)
{
    header('Location: ' . $location);
}

 

At the the top of the page, After the following:

session_start(); 
include ('db.php');
$li = db('');

Link to comment
Share on other sites

Thats awesome thanks for all the help guys.

 

I didn't realise that if the php code was put before the html tags that it would still keep the html formatting.

 

Is that also a hack or is that just normal practice?

 

Thanks again.

Link to comment
Share on other sites

No that is just normal practice. No form of output, eg: HTML, whitespace or any form of text should be outputted before a call to any function that sends/requests headers eg: setcookie, session_start, header etc.

 

Output is formed when you either tell php to echo/print a string within the php tags or anything outside of the php tags.

 

Also I have modified my post with another suggestion.

Link to comment
Share on other sites

How does the php output keep the html formatting when it is called before the html?

 

You simply store any output produced by your logic within variables, then, echo those variables within the html. eg;

 

<body bgcolor="#CCCCCC">
  <?php echo $msg; ?>
</body>

Link to comment
Share on other sites

Ah yes I see that makes a lot more sense. I left the echo in by mistake instead of storing it as a variable and it still displayed properly that was confusing me.

 

I still am not sure why it was displaying properly when it wasn't stored in variables with the html formatting.

 

Thanks again.

Link to comment
Share on other sites

Which code are you referring to? Your old code (posted in your first post) or my code in this post?

 

If you mean my code and you are changing $msg = to echo then the code will still work as you have outputted the HTML after you have called the header function. It is ok to send output after a call to header but it is not ok if you  send output before a call to header.

Link to comment
Share on other sites

Thanks, does this apply to all cases or is t here any instance where the HTML formatting would be lost?

Not sure what you mean, can you post code examples or explain your question in more detail.

 

The general rule is to not place any form of output before a call to the header function (or any function that requires headers, eg: session_start, setcookie and header). However it is ok to output anything after a call to the header function (or any function that requires headers).

Link to comment
Share on other sites

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.