I'm writing a web shop. Here's the flow:
User chooses a product from shopitems.php
User is sent to additem.php?id=123, which adds some info to the database then uses header() to redirect them to the shopping basket page.
I set up additem.php to email me when a record is added. I get two emails - the first one has shopitems.php as its referrer, the other doesn't have a referrer.
If I remove the header call to shoppingbasket.php, the row is only inserted twice. But here's the catch: if I remove all the code from shoppingbasket.php and then redirect there, it still inserts the two rows and I still get two emails - this suggests that the INSERT query code isn't duplicated anywhere else.
Why on earth would calling header() at the end of a script cause it to run a second time? I can confirm that I don't include that file anywhere else or redirect to it from anywhere.
Code:
additem.php
Code: Select all
<?php
$query1 = "SELECT ProductPriceID FROM tblProductsPrice WHERE ProductID = '" . cleanGet("id") . "'";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_row($result1);
$uniqueUserID = $_SESSION['uuid'];
$productID = cleanGet("id");
$productPriceID = $row1[0];
$userIP = $_SERVER['REMOTE_ADDR'];
$query2 = "INSERT INTO tblShopTransactions (UserID, ProductID, ProductPriceID, NumberItems, SessionActive, ClientIP, DateAdded)
VALUES ('$uniqueUserID', '$productID', '$productPriceID', '1', '1', '$userIP', CURRENT_TIMESTAMP())";
$result2 = mysql_query($query2) or die(mysql_error());
// code to email me goes here
session_write_close();
header("Location: shoppingbasket.php");
exit;
?>