[ Team LiB ] Previous Section Next Section

Hack 36 Share the Love (and Savings!) with Your Friends

figs/expert.giffigs/hack36.gif

Amazon's Share the Love program lets you and your friends save money by buying the same products. Here's a way to keep track of who wants what.

Amazon's "Share The Love" feature lets you send a 10% discount to people for the products you've purchased. For example, as you're checking out while purchasing The Matrix DVD, you're given the option to send a Share the Love discount via email. If anyone you send the discount to purchases the The Matrix DVD within a week, you'll receive a credit (the amount the 10% provides off of the regular price—so the more expensive an item, the bigger the credit).

The problem is, it's hard to know who among your friends is interested in the products you're purchasing. (Not to mention the struggle to remember their email address while you're checking out.)

This script comes to the rescue by gathering product requests from your friends, allowing all of you to share the savings with each other when you purchase products from Amazon. As you check out at Amazon, you can stop by this script to see if any of your friends have requested discounts for the products you're purchasing.

36.1 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 } ?>

36.2 Running the Hack

Upload this file to your server and visit the page. Get the ball rolling by entering your email address along with the ASIN of the products you'd like a discount for. Let your friends know about the page, and have them add products as well.

Now that you know who wants what, visit the page again as you're checking out of Amazon to see if anyone has requested discounts for the products you're buying. If they have, copy the list of addresses and paste them into Amazon's Share the Love form.

    [ Team LiB ] Previous Section Next Section