NON riesco a far funzionare i COOKIE!

  • Creatore Discussione Creatore Discussione be88
  • Data di inizio Data di inizio

be88

Nuovo Utente
23 Feb 2011
1
0
0
Salve a tutti!
Sono alle prime armi con php, e già combino i primi disastri:dipser:

Volevo esercitarmi un po' con i cookie, tra due file:

1) file cook3.php, in cui vi è una banalissima form in cui indicare un colore: premuto il tasto submit setto il cookie di nome QUARTOCOOK e di valore $pippo (=il colore scelto):
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Esercizi php</title> 
<link rel="stylesheet" type="text/css" href="" media="all"/> 
</head> 
<body> 
<?php 
    if(isset($_POST['submit'])){ 
    $pippo=$_POST['radio']; 
    setcookie("quartocook", $pippo); 
        } 
else{ 
    echo "<form action='cook4.php' method='post'>"; 
    echo "<input type='radio' name='radio' value='blue'/>Blu"; 
    echo "<input type='radio' name='radio' value='green'/>Verde"; 
    echo "<input type='radio' name='radio' value='yellow'/>Giallo"; 
    echo "<input type='submit' name='submit' value='vai'/>"; 
    echo "</form>"; 
    } 
?> 
</body> 
</html>

2) file cook4.php, in cui avrei voluto ricevere il valore del cookie settato nel file precedente, e semplicemente stamparlo a video:

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Esercizi php</title> 
<link rel="stylesheet" type="text/css" href="" media="all"/> 
</head> 
<body> 
<?php  
$colore= $_COOKIE['quartocook']; 
echo $colore; 
?> 
</body> 
</html>

cosa sbaglio??Perchè non funziona?
io uso MAMP su un mac...possibile che sia questione di configurazioni varie ed eventuali (safari, mamp, sistema??)?
Ringrazio chiunque vorrà perdere 5 minutini con me.... :crying:
 
Da http://php.net/manual/en/function.setcookie.php:

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Oltre a questo dato che il form presente in "cook3.php" ha come action "cook4.php", la parte di codice in cui è presente setcookie non viene in ogni caso eseguita.
 
Ultima modifica:

Discussioni simili