Foros del Web » Programando para Internet » PHP »

Problemas con header

Estas en el tema de Problemas con header en el foro de PHP en Foros del Web. no encuentro el fallo. Me da el siguiente error Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/tvm-e-learning.com/httpdocs/linse/registro.php:14) in /home/httpd/vhosts/tvm-e-learning.com/httpdocs/linse/registro.php ...
  #1 (permalink)  
Antiguo 03/08/2006, 07:51
Avatar de Kalili  
Fecha de Ingreso: enero-2002
Ubicación: Gate 69
Mensajes: 210
Antigüedad: 22 años, 3 meses
Puntos: 0
Problemas con header

no encuentro el fallo. Me da el siguiente error

Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/tvm-e-learning.com/httpdocs/linse/registro.php:14) in /home/httpd/vhosts/tvm-e-learning.com/httpdocs/linse/registro.php on line 122

Aunque ya se que los headers dan error si hay un output antes, que me aconsejariais hacer en este script?? Es un script de registro a una base de datos
muchas gracias. Decir que la linea 14 es donde empieza el <style>

Código PHP:
<?php
    
include("cookies.inc");
    include(
"instellingen.inc");
    
session_start();
    
    
database("connect");
?>
<!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>
<title>Linse B.V. Webshop</title>
<link rel="stylesheet" type="text/css" href="images/linse.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<style type="text/css">
.style1 {color: #FF0000}
</style>
</head>
<body>
<div align="center">
<table width="800" border="0" cellpadding="10" cellspacing="0"> 
    <tr> 
      <td bgcolor="#FFFFFF"><div id="DivPrincipal" name="DivPrincipal"> 
        <div id="DivCabecera" name="DivCabecera">
          <div align="center"><br />
            <br />
            <img src="images/top.jpg" width="780" height="120" /><br />
          </div>
        </div> 
        <table id="TablaPrincipal" cellspacing="0" cellpadding="0" width="100%" border="0"> 
          <tr> 
            <td class="PaginaActual" width="28%" rowspan="2">&nbsp;</td> 
            <td class="EstadoActual" valign="center" nowrap="nowrap" align="left"><div align="right">Your cart <a href="cart.php?action=showcart&categoriaID=<?=$_GET['categoriaID'];?>"><img src="images/carrito.gif" width="11" height="11" border="0" /></a> </div></td> 
          </tr> 
          <tr> 
            <td><table width="100%"  border="0" cellpadding="0" cellspacing="0"> 
                <tr> 
                  <td width="7"><img src="images/barra_izq.gif" width="7" height="6" /></td> 
                  <td><img src="images/barra_pixel.gif" width="100%" height="6" /></td> 
                  <td width="7"><img src="images/barra_der.gif" width="7" height="6" /></td> 
                </tr> 
              </table></td> 
          </tr> 
          <tr> 
            <td class="ContenidoMenu" valign="top" align="left"> <table width="100%"  border="0"> 
                <tr> 
                  <td class=EnlacesMenu bgcolor="#E8E8E8" onmouseover='this.style.background="#BDBCBC"' onmouseout='this.style.background="#E8E8E8"'><strong><a class=EnlacesMenu href="index.php?categoriaID=1">Handlieren</a></strong></td> 
                </tr> 
                <tr> 
                  <td class=EnlacesMenu bgcolor="#E8E8E8" onmouseover='this.style.background="#BDBCBC"' onmouseout='this.style.background="#E8E8E8"'><strong><a class=EnlacesMenu href="index.php?categoriaID=2">Electrische lieren</a></strong></td> 
                </tr> 
                <tr> 
                  <td class=EnlacesMenu bgcolor="#E8E8E8" onmouseover='this.style.background="#BDBCBC"' onmouseout='this.style.background="#E8E8E8"'><strong><a class=EnlacesMenu href="index.php?categoriaID=3">Hydraulische lieren</a></strong></td> 
                </tr> 
              </table></td> 
            <td class="ContenidoPrincipal" valign="top" align="left" rowspan="2"> 
            <br/> 
            <table width="640" border="0" cellspacing="0" cellpadding="10">
              <tr>
                <td>If you are not registered yet, you have to do it before you can go to the checkout. Thank you</td>
              </tr>
            </table>
            <br />
            <table width="640"  border="0" cellspacing="0" cellpadding="10">
              <tr>
              <?php
              
if ($_SERVER['REQUEST_METHOD'] == "POST") {
                  
$company $_POST["txtCompany"];
                
$name $_POST["txtNaam"];
                
$firstname $_POST["txtAchternaam"];
                
$login $_POST["txtLogin"];
                
$password $_POST["txtPassword"];
                
$datum date("F j, Y, g:i a");
                
$email $_POST["txtEmail"];
                
$adres $_POST["txtAdres"];
                
$cp $_POST["txtCp"];
                
$city $_POST["txtLocatie"];
                
$country $_POST["bill_country"];
                
$phone $_POST["txtTelefoon"];
                
$fax $_POST["txtFax"];
                
                if (
$company == ""){
                
$error "Company, ";}
                if (
$name == ""){
                
$error .= "Firstname, ";}
                if (
$firstname == ""){
                
$error .= "Lastname, ";}
                if (
$login == ""){
                
$error .= "Login, ";}
                if (
$password == ""){
                
$error .= "Password, ";}
                if ((!
ereg(".+\@.+\..+"$email)) || (!ereg("^[[email protected]]+$"$email))){
                
$error .= "E-mail, ";}
                if (
$adres == ""){
                
$error .= "Adress, ";}
                if (
$cp == ""){
                
$error .= "Postal code, ";}
                if (
$city == ""){
                
$error .= "City, ";}
                if (
$country == ""){
                
$error .= "Country, ";}
                if (
$phone == ""){
                
$error .= "Phone, ";}
                
                if(
$error){
                 
?>
                <td><font color="#FF0000">Please, fill in correct the following fields: <?php echo $error ?>thank you.</font></td>
                <?php
                
}
                else 
                {
                
$str_sql "INSERT INTO klanten
                                    (KLANT_COMPANY, KLANT_NAME, KLANT_FIRSTNAME, KLANT_LOGIN, KLANT_CONTRASENA, FECHA_KLANT_ALTA, KLANT_EMAIL, KLANT_ADRES, KLANT_LOCATIE, KLANT_CP, KLANT_LAND, KLANT_TELEFOON, KLANT_FAX)
                            VALUES
                                 ('$company', '$name', '$firstname', '$login', '$password', '$datum', '$email', '$adres', '$city', '$cp', '$country', '$phone','$fax')"
;
                
mysql_query($str_sql) or die("Error " mysql_error());
                
$sql "SELECT * FROM klanten WHERE KLANT_LOGIN = '$login' AND KLANT_CONTRASENA = '$password'";
                
$result mysql_query($sql) or die(mysql_error());
                if (@
mysql_num_rows($result) > 0) {
                        
$row mysql_fetch_assoc($result);
                        
$_SESSION['login_session']['idklant'] = $row['IDKLANT'];
                        
$idklant $_SESSION['login_session']['idklant'];
                        
mysql_query ("UPDATE cart SET IDKLANT = $idklant where IDCOOKIE = '" GetCartId() ."'");
                        }
                
mysql_close();
                
header("Location:buckaroo.php");
                }}
                
?>
              </tr>
              <tr>
                <td><form action="" method="post" name="Form" id="Form">
                  <table width="100%" border="0">
                    <tr>
                      <td><strong>Company :</strong></td>
                      <td><input name="txtCompany" type="text" id="txtCompany" size="50" /> 
                        <span class="style1">*</span> </td>
                    </tr>
                    <tr>
                      <td width="19%"><strong>Firstname :</strong></td>
                      <td><input name="txtNaam" type="text" id="txtNaam" size="50" />
                        <span class="style1">*</span></td>
                    </tr>
                        <input type="submit" name="SEND" value="Submit" />
                        <input type="reset" name="Submit2" value="Reset" />
                    </td>
                    </tr>
                  </table>
                </form></td>
              </tr>
            </table>
          </tr> 
          <tr> 
            <td class="ContenidoMenu" valign="top" align="middle">&nbsp;</td> 
          </tr> 
          <tr> 
            <td class="ContenidoMenu" valign="top" align="middle"></td> 
            <td class="CeldaCopyright" valign="center" align="middle">copyright &copy; 2006 - General Equipment Linse bv</td> 
          </tr> 
        </table></td> 
    </tr> 
  </table> 
</div> 
</body>
</html>
__________________
saludos
  #2 (permalink)  
Antiguo 03/08/2006, 08:28
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
La redirección que haces:
header("Location:buckaroo.php");

Entre todo el HTML que ahí montas .. NO es correcta en ese punto.

Piensa que diseñas páginas HTML pero que las "genera" PHP .. y no en que "metes" PHP entre el HTML .. así que olvidate de conceptos como "esto para el "body" .. esto para el "head" y cosas así.

Tus validaciones PHP deben hacerse antes de tu código HTML pueda ser generado .. De hecho tu validación decide si corresponde que "hay que redireccionar a otra página" . .por ende .. la página que se pueda generar NO corresponde generarla. Con "Header()" de tipo "location" le indicas al navegador que cambie la página que estás viendo .. pero eso se envia -antes- de cualquier otro tipo de "salida" que puedas enviar al navegador (cliente).


Código PHP:
 <?php 
              
if ($_SERVER['REQUEST_METHOD'] == "POST") { 
                  
$company $_POST["txtCompany"]; 
                
$name $_POST["txtNaam"]; 
                
$firstname $_POST["txtAchternaam"]; 
                
$login $_POST["txtLogin"]; 
                
$password $_POST["txtPassword"]; 
                
$datum date("F j, Y, g:i a"); 
                
$email $_POST["txtEmail"]; 
                
$adres $_POST["txtAdres"]; 
                
$cp $_POST["txtCp"]; 
                
$city $_POST["txtLocatie"]; 
                
$country $_POST["bill_country"]; 
                
$phone $_POST["txtTelefoon"]; 
                
$fax $_POST["txtFax"]; 
                 
                if (
$company == ""){ 
                
$error "Company, ";} 
                if (
$name == ""){ 
                
$error .= "Firstname, ";} 
                if (
$firstname == ""){ 
                
$error .= "Lastname, ";} 
                if (
$login == ""){ 
                
$error .= "Login, ";} 
                if (
$password == ""){ 
                
$error .= "Password, ";} 
                if ((!
ereg(".+\@.+\..+"$email)) || (!ereg("^[[email protected]]+$"$email))){ 
                
$error .= "E-mail, ";} 
                if (
$adres == ""){ 
                
$error .= "Adress, ";} 
                if (
$cp == ""){ 
                
$error .= "Postal code, ";} 
                if (
$city == ""){ 
                
$error .= "City, ";} 
                if (
$country == ""){ 
                
$error .= "Country, ";} 
                if (
$phone == ""){ 
                
$error .= "Phone, ";} 
                 
                if(
$error){ 
                    
// Si tienes un error .. redireccionas a esta misma página .. pero con una variable de control para presentar el mensaje de error.
                    
header ("Location: ".$_SERVER['PHP_SELF']."?control=error");
                    exit; 
                } 
                else  
                { 
                
$str_sql "INSERT INTO klanten 
                                    (KLANT_COMPANY, KLANT_NAME, KLANT_FIRSTNAME, KLANT_LOGIN, KLANT_CONTRASENA, FECHA_KLANT_ALTA, KLANT_EMAIL, KLANT_ADRES, KLANT_LOCATIE, KLANT_CP, KLANT_LAND, KLANT_TELEFOON, KLANT_FAX) 
                            VALUES 
                                 ('$company', '$name', '$firstname', '$login', '$password', '$datum', '$email', '$adres', '$city', '$cp', '$country', '$phone','$fax')"

                
mysql_query($str_sql) or die("Error " mysql_error()); 
                
$sql "SELECT * FROM klanten WHERE KLANT_LOGIN = '$login' AND KLANT_CONTRASENA = '$password'"
                
$result mysql_query($sql) or die(mysql_error()); 
                if (@
mysql_num_rows($result) > 0) { 
                        
$row mysql_fetch_assoc($result); 
                        
$_SESSION['login_session']['idklant'] = $row['IDKLANT']; 
                        
$idklant $_SESSION['login_session']['idklant']; 
                        
mysql_query ("UPDATE cart SET IDKLANT = $idklant where IDCOOKIE = '" GetCartId() ."'"); 
                        } 
                
mysql_close(); 
                
header("Location:buckaroo.php"); 
                exit;
                }} 
                
?>
Usa todo este código PHP -antes de tu código HTML-

Para mostrar el mensaje de error si se produce done lo tenías ..
Código PHP:
if ($_GET['control']=="error"){
echo 
"mensaje de error";

Un saludo,
__________________
Por motivos personales ya no puedo estar con Uds. Fue grato haber compartido todos estos años. Igualmente los seguiré leyendo.
  #3 (permalink)  
Antiguo 03/08/2006, 08:47
Avatar de Kalili  
Fecha de Ingreso: enero-2002
Ubicación: Gate 69
Mensajes: 210
Antigüedad: 22 años, 3 meses
Puntos: 0
Cluster muchas gracias por tu rapidisima respuesta.
Ya no me da el problema del warning del header pero ahora no me muestra lo que contiene la variable error. Me parece que es porque al hacer esto
header ("Location: ".$_SERVER['PHP_SELF']."?control=error"); se pierden las variables
como lo arreglaria muchas gracias
__________________
saludos
  #4 (permalink)  
Antiguo 03/08/2006, 10:23
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
El link que ves en el URL es correcto?

Como se llama ese script? ... Prueba:
header ("Location: nombre_script.php?control=error");

En mi ejemplo .. paso la variable "control" al script en concreto con valor "error" .. podría ser cualquier otra cosa .. total es sólo usado como un "flag" para saber si llego a ese script por la redirección mia o llamada directa al script.

Un saludo,
__________________
Por motivos personales ya no puedo estar con Uds. Fue grato haber compartido todos estos años. Igualmente los seguiré leyendo.
  #5 (permalink)  
Antiguo 04/08/2006, 01:28
Avatar de Kalili  
Fecha de Ingreso: enero-2002
Ubicación: Gate 69
Mensajes: 210
Antigüedad: 22 años, 3 meses
Puntos: 0
Si la url es la correcta. .../registro.php?control=error.

Si te fijas tengo una variable $error que es la variable si hay campos vacios en el form. Pues al decir

if($error){header(("Location: nombre_script.php?control=error");}

la variable $error se me muestra vacia cuando la saco en HTML luego en

<td><font color="#FF0000">Please, fill in correct the following fields: <?php echo $error ?>thank you.</font></td>

Un saludo Cluster y gracias .Haber si me puedes ayudar
__________________
saludos
  #6 (permalink)  
Antiguo 04/08/2006, 06:31
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
Insisto .. tu variable es "control" .. y su valor "error".

Código PHP:
<td><font color="#FF0000">Please, fill in correct the following fields: <?php echo $_GET['control']; ?>thank you.</font></td>
El mensaje de error no corresponde .. ahí solo dices "error" ante un problema.

En ese caso, .. tendrías que concatenar tu "variable" $error al URL que compones!!!:

Código PHP:
header ("Location: ".$_SERVER['PHP_SELF']."?control=".$error"); 
Un saludo,
__________________
Por motivos personales ya no puedo estar con Uds. Fue grato haber compartido todos estos años. Igualmente los seguiré leyendo.
  #7 (permalink)  
Antiguo 04/08/2006, 07:14
Avatar de Kalili  
Fecha de Ingreso: enero-2002
Ubicación: Gate 69
Mensajes: 210
Antigüedad: 22 años, 3 meses
Puntos: 0
Muchisimas gracias cluster. funciona perfectamente :)
__________________
saludos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:06.