Salve a tutti,
sto realizzando un carrello in php. Il codice l'ho assemblato io pescando un po' dal libro di Kevin Yank e un po' chgiedendo aiuto qua sul forum.
La pagina prodotto che ho realizzato (ora contiene solo il meccanismo per aggiungere il prodotto al carrello) è la seguente:
Codice PHP:
<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
if (isset($_GET['buy'])) {
// Add item to the end of the $_SESSION['cart'] array
$IDaggiungi = $_GET['buy'];
$_SESSION['cart'][$IDaggiungi]++;
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
$prod_corrente = $_GET['id'];
?>
<head>
<title>Product</title>
</head>
<body>
Your shopping cart contains <?php echo count($_SESSION['cart']); ?> items.</p>
[url="cartMIO.php"]View your cart[/url]</p>
<table border="1">
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
echo '<tr>';
echo '<td>' . $_SESSION['cart'][$prod_corrente] . '</td>';
echo '<td>prod' . $prod_corrente . '</td>';
echo '<td>prezzo tot</td>';
echo '<td>[url="' . $_SERVER['PHP_SELF'] . '?id=' . $prod_corrente . '&buy=' . $prod_corrente . '"]Buy[/url]</td>';
echo '</tr>';
?>
</tbody>
</table>
All prices are in imaginary dollars.</p>
</body>
</html>
Se voglio la pagina col prodotto 1 accedo ad essa in questo modo:
www.miodominio.it/prod.php?id=1
Ora, badate solo a quello che è il collegamento visualizzato nella status bar quando sposto il mouse su "buy". Mi visualizza l'URL con due parametri (id e buy) contenenti l'identificativo del prodotto corrente, in questo caso 1.
Cliccando però su buy, la pagina che si apre (cioé la stessa che si ricarica) contiene i nomi id e buy ma ha perso i valori.
Secondo me questo succede nelle righe dalla 6 alla 12, e cioè:
Codice PHP:
if (isset($_GET['buy'])) {
// Add item to the end of the $_SESSION['cart'] array
$IDaggiungi = $_GET['buy'];
$_SESSION['cart'][$IDaggiungi]++;
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
Però non so come ovviare a questo. La costante SID è inserita (come indicato nel volume di Yank) per conservare l'ID di sessione anche quando il browser ha i cookies disabilitati.
Come faccio a risolvere il problema senza compromettere questa importante funzionalità?
Come faccio ritornare i valori che spariscono dall'URL?
Grazie... spero che qulcuno mi aiuti anche se il messaggio è un po' lungo