Problem with header () in PHP

4

I have this warning:

  

Warning: Can not modify header information - headers already sent

I do not know what may be wrong with header.

This is my code:

<!DOCTYPE html>
<!--
 *
 * @author Zarate
-->
<?php
include ('serv.php');

$fechaInicio=$_POST['fInicio'];
$fechaFin=$_POST['fFin'];

if (isset($_POST['excel'])) {
    
    //archivo
    header('Content-Type:text/csv; charset=latin1');
//    header('Content-Type:text/csv; charset=latin1');
    header('Content-Disposition: attachment; filename="Solicitud_De_Cuentas.csv"');
    
    //archivo saliente
    $salida=fopen('php://output', 'w');
    
    fputcsv($salida, array('Nombre', 'Apellidos', 'Nombre Completo', 'Alias E-mail', 'Titulo', 'Puesto', 'Delegacion', 'Departamento', 'Direccion', 'Ciudad', 'Estado', 'Telefono', 'Extencion', 'Matricula', 'CURP', 'Num. Seg Social', 'Servidor de correo actual', 'Forma de Acceso', 'Sist. Operativo de la PC', 'Nombre de la PC', 'pwd'));
    
    $csv=$conect->query("SELECT * FROM registros where Fecha BETWEEN '$fechaInicio' and '$fechaFin'");
    while ($filaR=$csv->fetch_assoc())
            fputcsv($salida, array($filaR['Nombre'],
                                    $filaR['Apellidos'],
                                    $filaR['Nombre Completo'],
                                    $filaR['Alias E-mail'],
                                    $filaR['Titulo'],
                                    $filaR['Puesto'],
                                    $filaR['Delegacion'],
                                    $filaR['Departamento'],
                                    $filaR['Direccion'],
                                    $filaR['Ciudad'],
                                    $filaR['Estado'],
                                    $filaR['Telefono'],
                                    $filaR['Extension'],
                                    $filaR['Matricula'],
                                    $filaR['CURP'],
                                    $filaR['Num. Seg Social'],
                                    $filaR['Servidor de correo actual'],
                                    $filaR['Forma de Acceso'],
                                    $filaR['Sist. Operativo de la PC'],
                                    $filaR['Nombre de la PC'],
                                    $filaR['pwd']));
}
?>
    
asked by Eta Saints 08.01.2018 в 03:06
source

2 answers

5

PHP functions that send or modify HTTP headers must be executed before the requested page has been sent to the user. Otherwise, the following error will occur:

Warning: Cannot modify header information - headers already sent
(output started at file:line)

The PHP functions that modify the HTTP headers are the following:

header() / header_remove()
session_start() / session_regenerate_id()
setcookie() / setrawcookie()

And the ways to start sending content to the user before these functions are executed can be intentional or unintentional:

Intentional:

  • Show information with print or echo
  • Dump the content of variables with var_dump()
  • Use any of these functions: printf(), trigger_error(), vprintf(), ob_flush(), readfile() o passthru() .
  • Add HTML code before the%% open% tag

Unintentional:

  • Add some blank space before <?php or after <?php
  • The BOM (Byte Order Mark) of UTF-8
  • Error messages or notices produced previously
answered by 08.01.2018 / 11:31
source
1

The message of

  

Warning: Can not modify header information - headers already sent

It is sent because you are sending extra information or changing the information that is sent in the headers of the HTTP response when you have already sent previous content information. In the very specific case of your code, it is because you are sending xml tags in the first 5 lines of your code

<!DOCTYPE html>
<!--
 *
 * @author Zarate
-->

And then you are wanting to change the headers information to indicate that you are going to send a downloadable csv file with these other lines of code.

header('Content-Type:text/csv; charset=latin1');
header('Content-Disposition: attachment; filename="Solicitud_De_Cuentas.csv"');

Delete the first 5 lines of your code so that the warning message no longer appears.

    
answered by 03.04.2018 в 03:23