session start()

  • Creatore Discussione Creatore Discussione adfadf
  • Data di inizio Data di inizio

adfadf

Utente Attivo
18 Apr 2009
83
0
0
Sto utilizzando le sessioni, e mi necessita l'inserimento di session start () all'inizio di un file.php
leggo il senguente worm:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
cosa dobvrei fare???
 
Ma la funzione session_start() è proprio all'inizio del file o c'è qualcosa prima? E se è all'inizio del file, non è che questo file viene incluso da un altro script?

La causa dell'errore è la stessa di questo post, e la risposta anche.
 
Ho provato a fare quello che dievi, ma il punto è questo: attualmente lavoro su un portatile attraverso XAMPP, gli script erano già stati creati e provati su un fisso ed andava tutto bene.... Quello che mi colpiscecolpisce è il tipo di messaggio di errore
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
 
Nel messaggio di errore non c'è anche una parte che comincia con "output started at ..."? Che cosa dice?
 
Anche se nel messaggio è presente il riferimento al limite di cache il problema è dovuto comunque alla presenza di un output prima di session_start() (dai uno sguardo qui).
Dovresti postare un pò di codice, diversamente sarà difficile individuare il bug.
 
il messaggio di errore è:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\seleziona area.php:7) in C:\Program Files\xampp\htdocs\optionclass.php on line 2

questo è il codice di seleziona area.php
PHP:
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Titolo</title>
    <?php
    include "optionclass.php";
    $OpMenu = new OptionMenu();
    $OpMenu->JsHeader();
    // Abbiamo incluso la classe, istanziato la classe, e stampato la funzione Javascript
    ?>
	<b> <FONT FACE="Verdana, Arial, Helvetica, sans-serif" COLOR="#FF0000" SIZE="5"> Scegli in che sezione del sito mettere in vendita il tuo oggetto </FONT></b><p>
</head>
<br>
<br>
<body bgcolor='#FAEBD7'>
<p align="center">
<b> <FONT FACE="Verdana, Arial, Helvetica, sans-serif" COLOR="#FF0000" SIZE="2">Scegli la grande categoria</FONT></b>
<br>
<?php
$OpMenu->ShowGrandicategorie();
?>
<br>
<br>
<p align="center">
<b> <FONT FACE="Verdana, Arial, Helvetica, sans-serif" COLOR="#FF0000" SIZE="2">Scegli la categoria</FONT></b>
<br>
<?php
$OpMenu->ShowCategorie();
?>
<br>
<br>
<p align="center">
<b> <FONT FACE="Verdana, Arial, Helvetica, sans-serif" COLOR="#FF0000" SIZE="2">Scegli la sotto categoria</FONT></b>
<br>
<?php
$OpMenu->ShowSottocategorie();
?>

</body>
</html>

e questo è optionclass:
PHP:
<?php
session_start();

class OptionMenu
{
    protected    $conn;
    protected    $page;
    
        public function __construct()
        {
            $this->page = basename($_SERVER['PHP_SELF']);
            $this->DbConnectAndSelect();
            $this->ParsingAction();
            
        }
        
        protected function DbConnectAndSelect()
        {
            include "config_prova.php";
            
            $this->conn = @mysql_connect($host,$username) OR
            die("Impossibile connettersi al server");
            
            @mysql_select_db($db, $this->conn) OR
            die("Impossibile selezionare il database");
        }
        
        public function JsHeader()
        {
            echo '<SCRIPT language=JavaScript>
            <!--
            function goSelect(daform)
            {
                with(daform)
                {
                    top.window.location=options[selectedIndex].value;
                }
            }
            //-->
            </SCRIPT>
            ';

        }
        
        public function ShowGrandicategorie()
        {
            if(isset($_SESSION['grandecategoria']))
            {
                $grandecategoria = $_SESSION['grandecategoria'];
            }
            else
            {
                $grandecategoria = "Seleziona una grandecategoria";
            }
            
            $sql = "SELECT id,grandecategoria FROM grandicategorie";
            $res = mysql_query($sql, $this->conn);
            
            echo '<form>
            <select onchange="goSelect(this)" size="1">
            <option>' . $grandecategoria . '</option>
            ';
            
                while($row = mysql_fetch_array($res))
                {
                    if($row['grandecategoria'] != $grandecategoria)
                    {
                        echo '<option value="' . $this->page . '?codgrandecategoria=' . $row['id'] . '&action=categoria&grandecategoria=' . $row['grandecategoria'] . '">' . $row['grandecategoria'] . '</option>
                        ';
                    }
                }
            
            echo '</select>
            </form>
            ';
        }
        
        public function ShowCategorie()
        {
            if(isset($_SESSION['categoria']))
            {
                $categoria = $_SESSION['categoria'];
            }
            else
            {
                $categoria = 'Seleziona una categoria';
            }
            
            echo '<form>
            <select onchange="goSelect(this)" size="1">
            <option>' . $categoria . '</option>
            ';
            
            if($grandecategoria = $this->SelectCategoria())
            {
                $sql = "SELECT id,categoria FROM categorie WHERE codgrandecategoria='$grandecategoria'";
                $res = mysql_query($sql, $this->conn);
                
                    while($row = mysql_fetch_array($res))
                    {
                        if($row['categoria'] != $categoria)
                        {
                            echo '<option value="' . $this->page . '?codcategoria=' . $row['id'] . '&action=sottocategoria&categoria=' . $row['categoria'] . '">' . $row['categoria'] . '</option>
                        ';
                        }
                    }
            }
            
                echo '</select>
            </form>
            ';
        }
        
        public function ShowSottocategorie()
        {
            if(isset($_SESSION['sottocategoria']))
            {
                $sottocategoria = $_SESSION['sottocategoria'];
            }
            else
            {
                $sottocategoria = 'Seleziona una sottocategoria';
            }
            
            echo '<form>
            <select onchange="goSelect(this)" size="1">
            <option>' . $sottocategoria . '</option>
            ';
            
            if($_GET['codcategoria'])
            {
                $sql = "SELECT id,sottocategoria FROM sottocategorie WHERE codcategoria='$_GET[codcategoria]'";
                $res = mysql_query($sql, $this->conn);
                
                    while($row = mysql_fetch_array($res))
                    {
                        echo '<option value="' . $this->page . '?codsottocategoria=' . $row['id'] . '&action=end&sottocategoria=' . $row['sottocategoria'] . '">' . $row['sottocategoria'] . '</option>
                    ';
                    }
            }
            
                echo '</select>
            </form>
            ';
        }
        
        protected function SelectCategoria()
        {
            if(!$_GET['codgrandecategoria'] AND !$_SESSION['grandecategoria'])
            {
                return FALSE;
            }
            else
            {
                if($_GET['codgrandecategoria'])
                {
                    return $_GET['codgrandecategoria'];
                }
                else
                {
                    $sql = "SELECT id FROM grandicategorie WHERE grandecategoria='$_SESSION[grandecategoria]'";
                    $res = mysql_query($sql, $this->conn);
                    $row = mysql_fetch_array($res);
                    
                    return $row['id'];
                }
            }
        }
        
        protected function ParsingAction()
        {
            if(isset($_GET['action']))
            {
                if($_GET['action'] == 'categoria')
                {
                    $_SESSION['grandecategoria'] = $_GET['grandecategoria'];
                    unset($_SESSION['categoria']);
                    unset($_SESSION['sottocategoria']);
                }
                if($_GET['action'] == 'sottocategoria')
                {
                    $_SESSION['categoria'] = $_GET['categoria'];
                    unset($_SESSION['sottocategoria']);
                }
                if($_GET['action'] == 'end')
                {
                    $_SESSION['sottocategoria'] = $_GET['sottocategoria'];
					$_SESSION['codsottocategoria'] = $_GET['codsottocategoria'];
                    header("Location: form prova.php");
                    die;
                }
            }
        }
}
?>
 
Precisamente, il file optionclass.php che richiama la funzione session_start() è incluso dal file seleziona area.php. Il problema è che prima di includere il file optionclass.php, viene generato un output dal file seleziona area.php.
In pratica per funzionare dovresti richiamare la funziona session_start() prima di "<html><head> etc..."

Il fatto che su un altro PC non desse errori può dipendere dal fatto che la direttiva output_buffering fosse impostata su "On", permettendo di usare funzioni di modifica degli headers anche dopo aver generato un output (che viene bufferizzato).

Oppure sull'altra configurazione non era impostata la visualizzazione dei warnings...
 

Discussioni simili