The Code
This PHP script performs two separate actions depending on what
variables are passed to it. If someone is adding a product they'd
like to receive a discount on, it adds their email address to a text
file. (The file is written to the same directory as the script with
the name of the ASIN as the title,
ASIN.db.) If someone
is looking for email addresses to send "the love" (discount) to, it
displays all of the email addresses saved in the ASIN text file.
Create a PHP file called
share_love.php
and add the following code:
<?php
// share_love.php
// A Web page that associates ASINs with email addresses
// and then displays them. This helps take advantage of
// Amazon's *Share the Love* discount system.
if ($HTTP_GET_VARS) {
if ($HTTP_GET_VARS['sub']=="add item") {
$file=($HTTP_GET_VARS['asin'] . ".db");
$fp=fopen("$file", "a+");
fwrite($fp, $HTTP_GET_VARS['email']);
fclose($fp);
echo "Asin added!<br><br>";
} elseif ($HTTP_GET_VARS['sub']=="get addresses") {
$file=($HTTP_GET_VARS['asin'] . ".db");
if (file_exists($file)) {
echo "<h2>Sharing Love</h2>";
echo "Paste these addresses into Amazon's Share the Love";
echo " form:<br><br>";
$fp=fopen("$file", "r");
$contents = fread ($fp, filesize ($file));
$a_adds = split("\n", $contents);
if (count($a_adds) > 1) {
for($i = 0;$i < count($a_adds)-1; $i++) {
echo $a_adds[$i] . ", ";
}
} else {
echo $contents;
}
} else {
echo "<h2>No Love</h2>";
echo "No email addresses for this ASIN.";
}
}
} else {
?>
<html>
<body>
<h2>Add Product</h2>
<form action="share_love.php" method="get">
ASIN:<br><input type="text" name="asin" size="10"><br><br>
Email:<br><input type="text" name="email" size="25"><br><br>
<input type="submit" name="sub" value="add item">
</form>
<br><br>
<h2>Get Addresses</h2>
<form action="share_love.php" method="get">
ASIN:<br><input type="text" name="asin" name="" size="10"><br><br>
<input type="submit" name="sub" value="get addresses">
</form>
</body>
</html>
<?php } ?>