password gates

Password gates (I have no idea why they are called "gates", by the way) allow you to add simple question and answer type games to your site without the need for a staff member to send rewards manually. They're a great way to add interest without adding a lot of work.

This is a PHP script.
Your host must support PHP in order for you to use this script in your TCG.

complete page (aka sample template!)

PHP is the preferred method of creating password games because it is impossible for the player to cheat by viewing the source code of the page; the PHP code is hidden from the user.

Here's a complete but generic game page. There's no description of the game, just the coding.
Let's pretend that the file is called test.php.

*** YOUR PAGE HEADER (PHP includes, HTML, whatever) ***

<?php if(!isset($_POST['submit'])) { ?>

BLAH BLAH GAME DESCRIPTION

<form action="test.php" method="post">
     <input type="text" name="answer1" size="20">
     <input type="submit" value="Guess!" name="submit">
</form>

<?php } else{

$answer2 = "ANSWER";

if ($_POST["answer"]=="$answer2") 
  { ?>

WIN GAME MESSAGE & PRIZES

  <? } else {
  echo "Sorry, but that was the wrong answer.  Please try again!";
  } } ?>

*** YOUR PAGE FOOTER (PHP includes, HTML, whatever) ***

Okay. I know you're looking at this and saying "what the *BLEEP* is that mess?" but it's actually not that bad. I promise. I showed the entire page upfront so you could see how all the pieces fit together. Feel free to steal that sample to use as a template for your own games once you see how it works!

See it in action! View the test password gate page HERE!

Let's take it piece by piece.

explaining the PHP pieces

The explanation of each piece comes AFTER the brown box.
I think it makes more sense to have the explanation follow the example rather than come before it.

---

<?php if(!isset($_POST['submit'])) { ?>

This sets up the format of the page and allows for answer checking.
You NEVER need to change this line; it is used as-is, no editing!

---

BLAH BLAH GAME DESCRIPTION

Naturally, you insert your game description, images, and other HTML things here. You can use any HTML tags that you wish; they will NOT interfere with the operation of the PHP coding. Breaks, bold, italic, headers, image tags, you name it (even other PHP)!

---

<form action="test.php" method="post">
     <input type="text" name="answer" size="20">
     <input type="submit" value="Guess!" name="submit">
</form>

This creates the form that your members will type information into.

Pieces that you might (or DEFINITELY) need to edit are:
action="test.php?answer" - Change test.php to the name of your file (scramble.php, for example).
name="answer" - You can rename this to match your game (scramble1).
size="20" - Increase or decrease the size depending on the length of the answer.
value="Guess!" - The text displayed on your form's submit button.

---

<?php } else{

This tells the browser that everything following is part of the ANSWER section.
Again, you NEVER need to change this line. So don't do it. =p

---

$answer2 = "ANSWER";

This is where you enter your answers! For example, if the correct answer is "fishcakes" you'd replace "ANSWER" with "fishcakes". =p

---

if ($_POST["answer1"]=="$answer2") 
  { ?>

Finally, the actual answer check! Please look carefully. This is comparing the POSTED values taken from the form and comparing them to the ANSWER values that you just defined. It's checking to see if your member's entry for the "answer1" part of the form matches YOUR "answer2" answer.

You can rename your variables to whatever you like! However, I recommend keeping them all the same, but with different numbers at the end. It just helps avoid confusion.

---

WIN GAME MESSAGE & PRIZES

Once again, you are free to input any HTML or PHP that you wish in order to congratulate the member and provide prizes. You can add the coding for your randomizer to give random cards; it won't hurt the password PHP!

---

  <? } else {
  echo "Sorry, but that was the wrong answer.  Please try again!";
  } } ?>

This allows for a simple error message if the member enters the wrong information. The rest of the punctuation and mess is closing out the PHP to avoid browser errors.

multiple answer fields

Okay, so the sample shows how to make a password gate with ONE answer field. What if you want two, three, or ten? You can customize for as many as you want! The box below shows a sample of a password gate with THREE answer fields.

*** YOUR PAGE HEADER (PHP includes, HTML, whatever) ***

<?php if(!isset($_POST['submit'])) { ?>

BLAH BLAH GAME DESCRIPTION

<form action="test.php" method="post">
     <input type="text" name="answer1a" size="20">
     <input type="text" name="answer2a" size="20">
     <input type="text" name="answer3a" size="20">
     <input type="submit" value="Guess!" name="submit">
</form>

<?php } else{

$answer1b = "aaa";
$answer2b = "bbb";
$answer3b = "ccc";

if ($_POST["answer1a"]=="$answer1b" && $_POST["answer2a"]=="$answer2b" 
&& $_POST["answer3a"]=="$answer3b") 
  { ?>

WIN GAME MESSAGE & PRIZES

  <? } else {
  echo "Sorry, but that was the wrong answer.  Please try again!";
  } } ?>

*** YOUR PAGE FOOTER (PHP includes, HTML, whatever) ***

The form changes to include three fields called answer1a, answer2a, and answer3a. The answer section now has three matching variables, answer1b, answer2b, and answer3b. Finally, the answer check compares answer1a to answer1b, answer2a to answer2b, and answer3a to answer3b. Prizes will be given ONLY if all three form entries match the three answers you entered!

The most confusing part is this line:
if ($_POST["answer1a"]=="$answer1b" && $_POST["answer2a"]=="$answer2b" && $_POST["answer3a"]=="$answer3b")

The statement is linked together by && symbols which mean (amazingly), AND! So it's saying:
IF answer1a equals answer1b, AND answer2a equals answer2b, AND answer3a equals answer3b

give it a shot!

I know this looks intimidating, but I hope my explanation has been adequate. Try it out and see how it goes! If you break the code, you can always come back here and grab a fresh copy. Once you're feeling confident with it, try plugging the code into your layout and see if you can make it work with your own PHP headers, footers, and any randomizer scripts that you might be using.