Intro: PHP: the Gift Giver
As Christmas approaches, people scatter stores to get last-minute gifts. So I decided to decrease your risk of being trampled by people richer than you by creating a "gift giver" you can use online. It also has an "exchange" feature so you can get rid of your coffee maker (that you already have two of) without leaving the warmth of home. Be careful, though. You never know what it's going to give.
This system uses three files and sessions. The first file is just a cover page. It only has text and a button to send you to the second page. The second page give you an initial gift and an "exchange" option. The third page gives you a second gift if you chose to on page two. I will not include any stylesheets.
Step 1: Index.php
Index.php is the file apache sends to a client when they visit a site. You can actually rename this file to "index.html", "home.html", "home.php", etc. Just remember to make the necessary changes later.
In the root directory of your web server (named "www"), create two folders called "gift1" and "gift2". These folders will have one file in them called "index.php". Unlike the "index.php" file we are about to create, these index files HAS to have the "php" extension.
In your root directory, create "index.php" and put in the following text, then save the file:
<!DOCTYPE HTML><html><title>gift giver</title><meta charset="UTF-8"></head>
<h1>The Gift Giver</h1>
<p>Hahaha. This page will give you a gift. It is simple. Just click "give me
a gift." It will. It is a small gift. But you have a chance to "exchange" it
for another gift. The exchange gifts will be bigger. However, it may not be
a good gift.</p>
<br /><br />
<form action="./gift1" method="post">
<!-- Gift? <input type="checkbox" name="gift" value="yes" /><br /> -->
<input type="submit" name="ubmit" value="Click here for your gift" />
Step 2: Gift1/index.php
Now go to the "gift1" folder, create an "index.php" with the following text:
<!DOCTYPE HTML><html><head><title>Gift 1</title><meta charset="UTF-8"></head><body>
$g1ft=array("pie","teddy bear","baggie of sporks","bag of ice","backpack","bag of candy","hug","watch");//gift one comes from here; edit as desired
echo "<p>You have recieved a " . $x . " as a gift.</p>";
<p>Would you like to take a chance and exchange it for a new one?</p>
<br /><br />
<form action="../gift2/" method="post">
Yes: <input type="radio" name="exchange" value="yeah" /> No: <input type="radio" name="exchange" value="nope" /><br />
<input type="submit" value="Exchange/Keep" /></form>
Step 3: Gift2/index.php
This is the final file. Go to the "gift2" folder and create an "index.php" with the following text:
<!DOCTYPE HTML><html><title>gift</title><meta charset="UTF-8"><body>
$gift2=array("a car","a box of crap","a gallon of water","nothing","a laptop computer","a collection of pet rocks",
"some bad advice which you follow","toothpaste","an empty box","bad news: you got robbed",
"a court summons","a punch to the face","concert tickets; they're fake","a brand new coffee maker",
"a five square inch picture frame"); //this is the second gift array. Gift two will come from here. Edit as desired.
echo "<p>You have recieved " . $gift2[$num] . ".</p>";
echo "<p>You have chosen to keep the " . $_SESSION['gift'] . ".</p>";
echo "<p>Forget you and your " . $_SESSION['gift'] . "!</p>";
Step 4: Test It Out
Now you can just run through it a few times and look at how it all works. With the "g1ft" arrays, do as you please. Just remember that values are separated by commas and placed inside double quotes. Have fun.
Here is a tar.gz file with everything I used in this instructable: http://dl.dropbox.com/u/84764842/giftgiver.tar.gz
Step 5: Possible Issues and Fixes
1. I get session errors and/or other errors
It works on actual servers. For some reason, virtual servers act way differently from real servers
2. But I still get errors on a real server
Make sure the quotes and double quotes stay quotes. Sometimes characters get transformed when copying from a web browser
3. I get a "404" error when switching from "gift1" to "gift2", or from "gift2" to web root.
To get from "gift1" to "gift2", make sure the form action in "gift1" is set to "../gift2" with both periods. To get from "gift2" back to web root, make sure the link is set to go to "../" with both periods. Two periods tells the browser to go up one level, whereas one period tells the browser to stay within it's current level.
4. My browser shows the php code without executing any of it
You have to put these files into a web-server, either virtual or real.
5. My text editor opens these files unless I tell my browser to open them