Jump to content

Header may not contain more than a single header, new line detected.


.chester

Recommended Posts

Hello everyone, I'm just starting out with PHP as I need to create an online bookstore for a school project. I'm working by a magazine which should teach you exactly how to do this using PHP, but I've had a bunch of problems with the code they use and I don't really know what's going on. Anyway, this looks really simple and basically  what it does is allows you to post a comment on a book, then returns you to the book's page. Problem is, I'm getting the Header may not contain more than a single header, new line detected. error and I can't figure out why. I've tried researching into the matter but all the cases I found had to do with returning to an url, which is not my case.

 

Anyway, here's the snippet of code:

 

The form:

 

<div style="width:400px; border:1px solid #ffffff; background-color:#F9F1E7; padding:5px"> 
		<b>Adauga opinia ta:</b>
		<hr size="1">
		<form action="adauga_comentariu.php" method="POST">
			Nume: <input type="text" name="nume_utilizator"><br><br>
			Email: <input type="text" name="adresa_email"><br><br>
			Comentariu: <br> <textarea name="comentariu" cols="45"></textarea><br><br>
			<input type="hidden" name="id_carte" value="<?=id_carte?>">
			<center><input type="submit" value="Adauga"</center>
		</form>
	</div>

 

The script adaugare_comentariu.php:

 

 

<?php
ob_start();
include("conectare.php");
$numeFaraTags=strip_tags($_POST['nume_utilizator']);
$emailFaraTags=strip_tags($_POST['adresa_email']);
$comentariuFaraTags=strip_tags($_POST['comentariu']);
$sql="insert into comentarii (id_carte, nume_utilizator, adresa_email, comentariu) values(".$_POST['id_carte'].", '".$numeFaraTags."','".$emailFaraTags."','".$comentariuFaraTags."')";
mysql_query($sql);
$inapoi="carte.php?id_carte=".$_POST['id_carte'];
header("location:urldecode($inapoi)");
ob_end_flush();
?>

 

conectare.php connects to the mysql database. $inapoi is the variable which returns the user to carte.php (the book he posted a comment on), where id_carte is the book's unique id. I'm getting Header may not contain more than a single header, new line detected on line ten, which is the header line.

 

Can anyone help me? I've been stumped on this for a few days now and I've just let it pass and started working on other bits, but it's bugging me too much and I'd like to fix it.  :-[

The variable $inapoi likely contains a newline char, you cannot put a newline in a header via php. Also, I'm not sure what your trying to do with urldecode within your header call. A location header should be a complete and absolute url. This includes the hostname. eg;

 

header("Location: http://somedomain.com/foo/bar");

Is the newline char inside the carte.php file which is referred to in the $inapoi variable? Should I post carte.php too?

 

And if that doesn't work, is there any other way around this? Could I perhaps return the user to the book they just commented on in some other way?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.