Foros del Web » Programando para Internet » PHP »

Headers! Fastidiosos Headers!

Estas en el tema de Headers! Fastidiosos Headers! en el foro de PHP en Foros del Web. Hola a todos!, espero que una vez mas puedan echarme una mano, resulta que estoy teniendo un problemita con php siempre, SIEMPRE que envio headers, ...
  #1 (permalink)  
Antiguo 09/02/2009, 07:41
 
Fecha de Ingreso: julio-2008
Ubicación: very high
Mensajes: 41
Antigüedad: 15 años, 9 meses
Puntos: 0
Headers! Fastidiosos Headers!

Hola a todos!, espero que una vez mas puedan echarme una mano,
resulta que estoy teniendo un problemita con php siempre, SIEMPRE que envio headers, tengo por ejemplo el siguiente codigo con el que se supone podria generar un archivo en excell.

Código:
<?php require_once('Connections/Myconection.php'); ?>
<?php session_start(); ?>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>
<body>
<?php mysql_select_db($database_Myconection, $Myconection);
$sql = "SELECT clientes.nombre,  preguntas.indicador, preguntas.tipo, preguntas.ask, puntaje.observaciones, puntaje.suma, puntaje.puntos
FROM clientes, preguntas, puntaje
WHERE puntaje.cliente=clientes.nit AND puntaje.pregunta=preguntas.id_pr  AND preguntas.iden=19";
$r =  mysql_query($sql) or trigger_error(mysql_error($Myconection),E_USER_ERROR); 
$return = '';
if( mysql_num_rows($r)>0){
    $return .= '<table border=1>';
    $cols = 0;
    while($rs = mysql_fetch_row($r)){
        $return .= '<tr>';
        if($cols==0){
            $cols = sizeof($rs);
            $cols_names = array();
            for($i=0; $i<$cols; $i++){
                $col_name = mysql_field_name($r,$i);
                $return .= '<th>'.htmlspecialchars($col_name).'</th>';
                $cols_names[$i] = $col_name;
            }
            $return .= '</tr><tr>';
        }
        for($i=0; $i<$cols; $i++){
            if($cols_names[$i] == 'fechaAlta'){ #Fromateo el registro en formato Timestamp
                $return .= '<td>'.htmlspecialchars(date('d/m/Y H:i:s',$rs[$i])).'</td>';
            }else if($cols_names[$i] == 'activo'){ #Estado lógico del registro, en vez de 1 o 0 le muestro Si o No.
                $return .= '<td>'.htmlspecialchars( $rs[$i]==1? 'SI':'NO' ).'</td>';
            }else{
                $return .= '<td>'.htmlspecialchars($rs[$i]).'</td>';
            }
        }
        $return .= '</tr>';
    }
    $return .= '</table>';
    mysql_free_result($r);
}
header("Content-type: application/vnd-ms-excel; charset=iso-8859-1");
header("Content-Disposition: attachment; filename=NombreDelExcel_".date('d-m-Y').".xls");
echo $return;?>
</body>
</html>
y me genera los siguientes errores:

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\Esatic\prueba.php:10) in C:\AppServ\www\Esatic\prueba.php on line 45

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\Esatic\prueba.php:10) in C:\AppServ\www\Esatic\prueba.php on line 46

Ya he visto en otros post y en otros foros, y dicen que pueden ser espacios en blanco pero aca no los tengo y las directivas de errores en php las tengo todas en off, y tampoco imprimo nada en pantalla antes de enviar los headers entonces que podria ser ??? alguna idea espero que puedan ayudarme que ya estoy bastante fastidiado con estos headers. Muchas Gracias por su tiempo.
  #2 (permalink)  
Antiguo 09/02/2009, 07:53
Avatar de lucasan
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: Cali - Colombia
Mensajes: 1.144
Antigüedad: 16 años, 10 meses
Puntos: 59
Respuesta: Headers! Fastidiosos Headers!

Cita:
Iniciado por fenixdemetal Ver Mensaje
dicen que pueden ser espacios en blanco pero aca no los tengo... y tampoco imprimo nada en pantalla antes de enviar los headers entonces que podria ser ???
Hola

Código HTML:
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>
<body> 
Aunque tu no vez nada en pantalla, esto se está enviando al navegador.

Saludos.
__________________
Plan Original Desarrollo Web
~$>sudo apt-get install -f
"Asi debería arreglarse todo en la vida"
  #3 (permalink)  
Antiguo 09/02/2009, 08:03
okram
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Headers! Fastidiosos Headers!

No te líes por gusto y coloca los headers siempre al comienzo de tu script.

FAQ 186. ¿Por qué me sale "Warning: Cannot modify header information - headers already sent by (output started at [1]:[2]) in [3] on line [4]"

  #4 (permalink)  
Antiguo 09/02/2009, 14:56
 
Fecha de Ingreso: julio-2008
Ubicación: very high
Mensajes: 41
Antigüedad: 15 años, 9 meses
Puntos: 0
Respuesta: Headers! Fastidiosos Headers!

Gracias por sus respuestas lucasan tiene razon y probare el consejo de okram, ya que estos headers son bastante molestos. !

Exitos!
  #5 (permalink)  
Antiguo 09/02/2009, 15:20
Avatar de SCY-FOX  
Fecha de Ingreso: septiembre-2007
Ubicación: Chile
Mensajes: 139
Antigüedad: 16 años, 7 meses
Puntos: 5
Respuesta: Headers! Fastidiosos Headers!

Cambia a esto:

Código PHP:
<?php require_once('Connections/Myconection.php'); ?>
<?php session_start
(); 

mysql_select_db($database_Myconection$Myconection);

header("Content-type: application/vnd-ms-excel; charset=iso-8859-1");
header("Content-Disposition: attachment; filename=NombreDelExcel_".date('d-m-Y').".xls");


$sql "SELECT clientes.nombre,  preguntas.indicador, preguntas.tipo, preguntas.ask, puntaje.observaciones, puntaje.suma, puntaje.puntos
FROM clientes, preguntas, puntaje
WHERE puntaje.cliente=clientes.nit AND puntaje.pregunta=preguntas.id_pr  AND preguntas.iden=19"
;
$r =  mysql_query($sql) or trigger_error(mysql_error($Myconection),E_USER_ERROR); 
$return '';
if( 
mysql_num_rows($r)>0){
    
$return .= '<table border=1>';
    
$cols 0;
    while(
$rs mysql_fetch_row($r)){
        
$return .= '<tr>';
        if(
$cols==0){
            
$cols sizeof($rs);
            
$cols_names = array();
            for(
$i=0$i<$cols$i++){
                
$col_name mysql_field_name($r,$i);
                
$return .= '<th>'.htmlspecialchars($col_name).'</th>';
                
$cols_names[$i] = $col_name;
            }
            
$return .= '</tr><tr>';
        }
        for(
$i=0$i<$cols$i++){
            if(
$cols_names[$i] == 'fechaAlta'){ #Fromateo el registro en formato Timestamp
                
$return .= '<td>'.htmlspecialchars(date('d/m/Y H:i:s',$rs[$i])).'</td>';
            }else if(
$cols_names[$i] == 'activo'){ #Estado lógico del registro, en vez de 1 o 0 le muestro Si o No.
                
$return .= '<td>'.htmlspecialchars$rs[$i]==1'SI':'NO' ).'</td>';
            }else{
                
$return .= '<td>'.htmlspecialchars($rs[$i]).'</td>';
            }
        }
        
$return .= '</tr>';
    }
    
$return .= '</table>';
    
mysql_free_result($r);
}

?>

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>
<body>
<?=$return;?>
</body>
</html>
  #6 (permalink)  
Antiguo 29/05/2012, 12:18
 
Fecha de Ingreso: octubre-2011
Mensajes: 1
Antigüedad: 12 años, 6 meses
Puntos: 0
Respuesta: Headers! Fastidiosos Headers!

Excelente me funciono de 10!!! Mil Gracias!!!
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 22:38.