Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805

    errore warning a causa ddegli header

    salve ragazzi sto usando una classe per fare il bakup del database , adesso avrei un piccolo problema , la classe non mi effettua il download del bakup , ma mi stampa a video il bakup e mi escono errori warning relativi agli header .......
    codice:
    Warning: Cannot modify header information - headers already sent by (output started at C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\supp\includes\Smarty\templates_c\%%CE^CE4^CE42039A%%cpheader.tpl.php:6) in C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\supp\includes\functions_backup.php on line 272
    perche ?

    posto la classe magari se mi dareste una dritta
    Codice PHP:
            class backup {
            var 
    $newline;
            var 
    $struct_only false;
            var 
    $output true;
            var 
    $compress true;
            var 
    $filename "";


            function 
    backup($struct_only false$output true$compress true$filename "" )
            {
                
    $this->output $output;
                
    $this->struct_only $struct_only;
                
    $this->compress $compress;

                
    $this->filename $filename;
                
    $this->newline $this->_define_newline();
            }

            function 
    _backup()
            {
                global 
    $dbcore ;
                
    $now gmdate'D, d M Y H:i:s' ) . ' GMT';

                
    $newfile .= "#########################################" $this->newline;
                
    $newfile .= "# Database Backup Class by Iván Melgrati" $this->newline;
                
    $newfile .= "# Database:"DATABASE_NAME  $this->newline;
                
    $newfile .= "# Date: $now$this->newline;
                
    $newfile .= "#########################################" $this->newline $this->newline;



                
    $result $dbcore->query"SHOW TABLES FROM `".$dbcore->escape(DATABASE_NAME)."`" );
                while ( list(
    $table) = $dbcore->fetch_row($result))
                {
                    
    $newfile .= $this->_get_def($table);
                    
    $newfile .= "$this->newline";
                    if ( !
    $struct_only )
                             
    $newfile .= $this->_get_content$table );
                    
    $newfile .= "$this->newline";
                    
    $i++;
                }

                
    $this->_out($newfile);
            }

            function 
    _out($dump)
            {
                 
                if ( 
    $this->filename ){
                    
    $fptr fopen($this->filename"wb" );
                    
                    if (
    $fptr){
                        
                         if(
    $this->compress)
                        {
                        
                            
    $gzbackupData "\x1f\x8b\x08\x00\x00\x00\x00\x00" substr(gzcompress($dump9), 0, -) . pack'V'crc32($dump) ) . pack'V'strlen($dump));
                            
    fwrite($fptr$gzbackupData);
                            
                          }else{ 
                          
                            
    fwrite($fptr$dump);
                            
                        }
                         
                        
    fclose($fptr);
                    }
                 }
                 else
                 {
                
                if((
    $this->compress) && ($this->output))
                {
                
                        
    $gzbackupData "\x1f\x8b\x08\x00\x00\x00\x00\x00" substrgzcompress($dump9), 0, -) . pack'V'crc32($dump) ) . pack'V'strlen($dump) );
                        echo 
    $gzbackupData;
                        
                }else{
                      
                        echo 
    $dump;
                        
               }
              }
            }

            function 
    _get_def($tablename)
            {
                global 
    $dbcore ;
                
    $def "";
                
    $def .= "#------------------------------------------" $this->newline;
                
    $def .= "# Table definition for $tablename$this->newline;
                
    $def .= "#------------------------------------------" $this->newline;
                
    $def .= "DROP TABLE IF EXISTS $tablename;" $this->newline $this->newline;
                
    $def .= "CREATE TABLE $tablename (" $this->newline;
                
    $result $dbcore->query"SHOW FIELDS FROM `".$dbcore->escape($tablename)."`" ) or die( "Table $tablename not existing in database" );
                while ( 
    $row $dbcore->fetch_array($result) )
                {
                    
                     
    $def .= " ".$row["Field"]."`".$row["Type"];   
                    if(
    $row["Null"] != "YES"){
                       
    $def .= " NOT NULL";
                    }
                    if(
    $row["Default"] != ""){
                        if ( 
    $row["Default"] == "CURRENT_TIMESTAMP" ){
                             
    $def .= " default CURRENT_TIMESTAMP";
                            }else{
                                
    $def .= " default $row[Default]";
                            }
                        }
                    if(
    $row['Extra'] != "" ){
                       
    $def .= $row[Extra]";
                       }
                       
    $def .= ",$this->newline";
                    }
                       
    $def ereg_replace",$this->newline$"""$def);


                
    $result $dbcore->query("SHOW KEYS FROM `".$dbcore->escape($tablename)."`" );
                while (
    $row $dbcore->fetch_array($result))
                {
                    
    $kname $row[Key_name];
                    if ( (
    $kname != "PRIMARY") && ($row[Non_unique] == 0) ) $kname "UNIQUE|$kname";
                    if ( !isset(
    $index[$kname]) ) $index[$kname] = array();
                    
    $index[$kname][] = $row[Column_name];
                }

                while (list(
    $x$columns) = @each($index) )
                {
                    
    $def .= ",$this->newline";
                    if ( 
    $x == "PRIMARY" $def .= "   PRIMARY KEY (" implode$columns", " ) . ")";
                    else if ( 
    substr($x06) == "UNIQUE" $def .= "   UNIQUE " substr$x) . " (" implode$columns", " ) . ")";
                    else  
    $def .= "   KEY $x (" implode$columns", " ) . ")";
                }
                
    $def .= "$this->newline);";

                return ( 
    stripslashes($def) );
            }

            function 
    _get_content$tablename )
            {
                global 
    $dbcore ;
                
    $content "";

                
    $result $dbcore->query("SELECT * FROM  `".$dbcore->escape($tablename)."`" );

                if (
    $dbcore->num_rows($result) > 0)
                {
                    
    $content .= "#------------------------------------------" $this->newline;
                    
    $content .= "# Data inserts for $tablename$this->newline;
                    
    $content .= "#------------------------------------------" $this->newline;
                }

                while (
    $row $dbcore->fetch_row($result))
                {
                    
    $insert "INSERT INTO `".$dbcore->escape($tablename)."` VALUES (";

                    for ( 
    $j 0$j $dbcore->num_fields($result); $j++ )
                    {
                        if ( !isset(
    $row[$j]) ) $insert .= "NULL,";
                        elseif ( 
    $row[$j] != "" $insert .= "'" addslashes$row[$j] ) . "',";
                        else  
    $insert .= "'',";
                    }

                    
    $insert ereg_replace",$"""$insert );
                    
    $insert .= ");$this->newline";
                    
    $content .= $insert;
                }

                return 
    $content $this->newline;
            }

            function 
    _define_newline()
            {
                
    $unewline "\r\n";

                if ( 
    strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'win') )
                {
                    
    $unewline "\r\n";
                }else if ( 
    strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'mac') )
                {
                  
    $unewline "\r";
                }else{
                        
    $unewline "\n";
                    }

                    return 
    $unewline;
            }

            function 
    _get_browser_type()
            {
                
    $USER_BROWSER_AGENT "";

                if ( 
    ereg('OPERA(/| )([0-9].[0-9]{1,2})'strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                {
                
    $USER_BROWSER_AGENT 'OPERA';
                }
                else if ( 
    ereg('MSIE ([0-9].[0-9]{1,2})'strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                {
                
    $USER_BROWSER_AGENT 'IE';
                }
                else if ( 
    ereg('OMNIWEB/([0-9].[0-9]{1,2})'strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                {
                
    $USER_BROWSER_AGENT 'OMNIWEB';
                }
                else if ( 
    ereg('MOZILLA/([0-9].[0-9]{1,2})'strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                {
                
    $USER_BROWSER_AGENT 'MOZILLA';
                }
                else if ( 
    ereg('KONQUEROR/([0-9].[0-9]{1,2})'strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                {
                
    $USER_BROWSER_AGENT 'KONQUEROR';
                }
                else
                {
                
    $USER_BROWSER_AGENT 'OTHER';
                }

             return 
    $USER_BROWSER_AGENT;
            }

            function 
    _get_mime_type()
            {
                
    $USER_BROWSER_AGENT $this->_get_browser_type();

                
    $mime_type = ( $USER_BROWSER_AGENT == 'IE' || $USER_BROWSER_AGENT == 'OPERA' ) ? 'application/octetstream' 'application/octet-stream';
                return 
    $mime_type;
            }

            function 
    perform_backup()
            {

                
    $now gmdate'D, d M Y H:i:s' ) . 'GMT';
                if ( 
    $this->compress )
                {
                    
    $filename $this->dbname ".sql";
                    
    $ext "gz";
                }else{
                    
    $filename $this->dbname;
                    
    $ext "sql";
                }

                
    $USER_BROWSER_AGENT $this->_get_browser_type();

                if ( 
    $this->filename )
                {
                    
    $this->_backup();
                }
                else if ( 
    $this->output == true )
                    {
                        
    header'Content-Type: ' $this->_get_mime_type() );
                        
    header'Expires: ' $now );
                        if ( 
    $USER_BROWSER_AGENT == 'IE' )
                        {
                            
    header'Content-Disposition: inline; filename="' $filename '.' $ext '"' );
                            
    header'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
                            
    header'Pragma: public' );
                        }
                        else
                        {
                            
    header'Content-Disposition: attachment; filename="' $filename '.' $ext '"' );
                            
    header'Pragma: no-cache' );
                        }

                        
    $this->_backup();
                    }
                    else
                    {
                        echo 
    "<html><body><pre>";
                        echo 
    htmlspecialchars$this->_backup() );
                        echo 
    "</PRE></BODY></HTML>";
                    }
            }
        } 
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    112
    Non ho avuto tempo di guardate tutto il codice, ma hov isto che nella classe vengono lanciati degli header (immagino quando lo script fa scaricare il file all'utente).

    L'errore che ti restituisce credo nasca propio da qui: quando lanci un header (come in questo script o quando per esempio fai session_start()) nessuno output deve essere gia stato inviatob.

    In pratica significa che quando richiamo il metodo backup (che poi lancerà gli header) prima non deve essere stata fatta nessuna echo, print o integrato codice html altrimenti l'hader genera l'errore.


    Gabriele

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    ciao e grazie , comunque credo sia qua il problema

    Codice PHP:
            function _out($dump
            { 
                 
                if ( 
    $this->filename ){ 
                    
    $fptr fopen($this->filename"wb" ); 
                     
                    if (
    $fptr){ 
                         
                         if(
    $this->compress
                        { 
                         
                            
    $gzbackupData "x1fx8bx08x00x00x00x00x00" substr(gzcompress($dump9), 0, -) . pack'V'crc32($dump) ) . pack'V'strlen($dump)); 
                            
    fwrite($fptr$gzbackupData); 
                             
                          }else{ 
                           
                            
    fwrite($fptr$dump); 
                             
                        } 
                         
                        
    fclose($fptr); 
                    } 
                 } 
                 else 
                 { 
                 
                if((
    $this->compress) && ($this->output)) 
                { 
                 
                        
    $gzbackupData "x1fx8bx08x00x00x00x00x00" substrgzcompress($dump9), 0, -) . pack'V'crc32($dump) ) . pack'V'strlen($dump) ); 
                        echo 
    $gzbackupData
                         
                }else{ 
                       
                        echo 
    $dump
                         
               } 
              } 
            } 
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  4. #4
    Utente di HTML.it L'avatar di Marcolino's
    Registrato dal
    May 2003
    residenza
    Udine
    Messaggi
    3,606
    A parte che lanciare del codice HTML da una classe non è sempre corretto perché si rischia di non chiudere la classe, ovvero di instanziarla e poi di lasciarla a metà strada perché magari il codice riportato redireziona il browser da un'altra parte, come nel tuo caso, dicevo a parte tutto questo, usa gli ob_ che ti permettono di far preparare tutti i tuoi script in memoria e poi lanciarli in un colpo solo.
    Nella maggior parte dei casi ti evita proprio i problemi da te enunciati e inoltre chiudono tutti gli script aperti liberando la memoria dei puntatori.
    Nel tuo caso ad esempio, nel file che instanzia la classe fai così:
    Codice PHP:
    <?php
    ob_start
    (true);
    include 
    'la_mia_classe.php';

    quel che viene viene


    on_end_flush
    ();
    ?>
    con true dentro ob_start() automaticamente rilasci tutte le variabili e i puntatori alla fine dello script. (se non ricordo male )

  5. #5
    Utente di HTML.it L'avatar di Marcolino's
    Registrato dal
    May 2003
    residenza
    Udine
    Messaggi
    3,606
    PS che non c'emntra col tuo problema, ma perché in ogni condizione ripeti sempre strtoupper($_SERVER["HTTP_USER_AGENT"]) ma non ti conviene richiamarlo una sola volta inserendo il risultato in una variabile?

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    grazie sei stato gentilissimo
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  7. #7

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.