If you've followed the instructions in Part 1, Part 2, and Part 3 of the tutorial, you should have an adoptables system where you can add a list of adoptables to a text file, have people randomly get different variations of their pet when they adopt, and have pets grow up automatically when the date changes.
One problem with the system so far is that people can tell which variation they ended up getting by looking at their adoption code. The adoption codes look like this:
http://www.mywebsite.com/simple.php?a=1&v=1
If one person got an adoptable with v=1, anyone else with v=1 can tell that they got the same pet. People can see which the rare pets are even before they grow up because they have a different code which is easy to spot. So, the goal for this tutorial is:
To do this, we'll be encrypting the "v=" part of the URL. In adopttools.php, we'll add routines to encrypt the variation ID, and to decrypt it. The file will now begin like this:
<?php
//Load the adoptables XML file
$adoptxml=simplexml_load_file("adoptables.xml");
require("key.php");
function encrypt_variation($id) {
global $encrypt_key;
$key=rand(0, 0xFFFF);
return ((($id ^ $key) << 16) | $key) ^ $encrypt_key;
}
function decrypt_variation($id) {
global $encrypt_key;
$id^= $encrypt_key;
return (($id >> 16) & 0xFFFF) ^ ($id & 0xFFFF);
}
The file "key.php" is where we store the encryption key for our website. Your key needs to be kept secret, because if people know it then they could use it to decrypt the variation IDs and find out which pet they ended up getting. Here's a key.php with a randomly-generated encryption key (refresh this page to get a new key):
<?php
$encrypt_key=0x292f19aa;
?>
Save that as key.php. Like simple.php and adopttools.php, it is important that there are no blank lines or spaces before the <?php or after the ?> of these files, so make sure that there are none when you create key.php.
The only other changes we need to make are in adopt.php (where it will need to encrypt the variation ID), and in simple.php (where it will need to decrypt the variation ID). Change this line in simple.php:
$variation=find_variation($_REQUEST['a'], $_REQUEST['v']);
To this:
$variation=find_variation($_REQUEST['a'], decrypt_variation($_REQUEST['v']));
Change this line in adopt.php:
$v_id=$variation['id'];
To this:
$v_id=encrypt_variation($variation['id']);
Upload the new adopttools.php, adopt.php, simple.php and key.php files. You can download all these files (except key.php which you should copy and paste from earlier on this page) in a zip file.
Check out the new adoption website. Notice that you get a different code nearly very time you adopt a pet, even if the variation is the same!
You can discuss this part of the tutorial in the forums, or move on to Part 5.