iD Tech Camps iD Tech Camps
REQUEST A BROCHURE



AMERICA'S #1 TECH CAMP

All posts by: KenK


Subscribe to the iD Blog

iD Blog Author: KenK

Ken has been involved with iD Tech for close to six years - two as an instructor and three as a full-time employee. He's taught courses related to everything from programming to game design to web design, but his passion is programming. Now he works and blogs full-time for iD, lives in Los Angeles, and as a hobby tries to mix stage comedy and programming to mostly confused audiences.

Making a Poll in PHP from Scratch: Part 1

May 14th, 2009 by KenK

There are plenty of ready-made Internet polls out there, particularly if you are using a framework for your site such as Wordpress, vBulletin, or Joomla/Drupal. But occasionally it is worthwhile to create your own from scratch, whether for learning or to simply fulfill specific features you need in your application. In this series of posts, that is exactly what we will create – a homemade PHP/MySQL voting page. You should be then able to modify this code for your specific needs.

First, let’s talk about the inherent flaws in Internet voting. In the most basic form, an Internet poll is just a choice a person makes on a page that is stored in the database. Without any checks, a person could just refresh the page and vote over and over again. Or, worse, they could employ a script of their own that does this automatically. You could potentially register millions of votes this way, given enough time. So what checks and balances do we have? There are three main options.

User Login

A user login system is the most secure of your options, especially if the accounts are linked to a unique email address. If you are using a system like Wordpress or Drupal, then it is trivial to store the userid of the voter in the database as well. Do you want to use this option with your poll? If the user is already logging in for other reasons, then this is probably fine. Making a user register just to vote in a poll brings the convenience factor way down, most likely deterring most people. Plus, is it really worth it? If a user has the time and inclination, they can simply register over and over again, especially if they have their own domain or email server from which to pull infinite accounts. You could employ some IP-checking with your account checks to make sure they are unique, but then it just becomes an arms race. This option is too burdernsome for us to use in this project.

Cookies

We could use cookies set on a person’s machine to determine whether they’ve voted or not, but all it takes is a browser switch, cleared cache, or a privacy mode and suddenly our checking goes right out the window. This option is almost too simple for the user to overcome.

IP Address

The last major option is an IP address check. There are several flaws with this method, like the others, but we can overcome a few of them more easily. First, IP addresses are not a one-address-to-one-person mapping. The system can go either way, in fact. You can have multiple IPs for a single person, if they have multiple systems, a cellphone, so on. You can also have multiple people for an IP, in the case of a corporate network. You could have 10,000 people registering as a single IP. But these are problems we can find workarounds for.  We’ll use IP checking in conjuction with a few tricks as our authentication method.

Database Structure: Your Favorite Fruit

For this poll, I’m creating two different database tables, mypoll and mypolltally. The first table will contain all of the choices in our poll and the second the individual votes. This means that we can only have a single poll at a time, but this is a trivial matter in the future to extend (we would simply add another column to mypoll, pollid, containing a unique identifier. Then we would just need a column in mypolltally to store the pollid of each vote).

         mypoll                                 mypolltally
--------------------------      --------------------------------------------
| id |      choice       |      | id | isvotefor |    ipaddress    | value |
--------------------------      --------------------------------------------
| 0  | banana            |      | 0  |     3     | 255.255.255.255 |   1   |
| 1  | apple             |      | 1  |     4     | 254.255.255.255 |   1   |
| 2  | orange            |      | 2  |     1     | 251.255.255.255 |   1   |
| 3  | strawberry        |      | 3  |     2     | 252.255.255.255 |   1   |
| 4  | blueberry         |      | 4  |     2     | 253.255.255.255 |   1   |
--------------------------      --------------------------------------------

The Fields

mypoll:

  • id: A unique identifier for records in the table. When we register a vote in mypolltally, we’ll need to know which option we are voting for. We’ll use this value to do so.
  • choice: The text for each option. Our poll question is “What is your favorite fruit?”, so here we lay out the choices such as
  • banana, apple, and orange.

mypolltally:

  • id: A unique identifier for each vote in the system.
  • isvotefor: This field points identifies which option in mypoll that this vote is selecting.
  • ipaddress:The IP address of the voter.
  • value:We can extend our features a little by having this field. Instead of simply registering a “Yes” for a particular option, we can register a range. We can then allow people to indicate how much they like the option. Perhaps a 1 value is a little, a 5 value is a lot, and anything negative means they do not like it. For now, a value of 1 will just mean yes but later we’ll extend it for enhanced voting.

That’s enough of the theory for now. In the next part of the series we’ll launch into the actual PHP code and finish up with the basic functionality. In Part 3 we’ll extend our code to allow for more features.

Randomizing your Flickr to vBulletin Feed

May 7th, 2009 by KenK

Last time I discussed pulling your Flickr feed into a vBulletin installation (or really, any php-based CMS/website). That solution pulled the lastest photos up to some amount and displayed them. But what if you want to randomize them and not always display the pictures in the same order? We’ll be pulling a large amount of photos from Flickr and I don’t always want only the last ones to show up in the same order. Instead, I’d like to pull the last, say, 200 photos and randomly display 10 of them. Here’s the code modification from last week’s post:

require_once("includes/phpFlickr.php");

// Create new phpFlickr object
$f = new phpFlickr("yourapikey");

$f->enableCache("db","mysql://user:password@server/database");

$limit = 200;
$count = 10;

// Find the NSID of the username inputted via the form
$person = $f->people_findByUsername('iDTechCamp');

// Get the friendly URL of the user's photos
$photos_url = $f->urls_getUserPhotos($person['id']);

// Get the user's first X($limit) public photos
$photos = $f->people_getPublicPhotos($person['id'], NULL, NULL, $limit);

$flickrbox = "<table class='flickrbox'><tr>";
$nonrepeatarray = array();

for ($i = 0; $i < $count; $i++) {
    $rand = rand(0,199);
    while(in_array($rand,$nonrepeatarray)) $rand = rand(0,199);
    array_push($nonrepeatarray, $rand);

    $photo = (array)$photos['photos']['photo'][$rand];
    $flickrbox .= "<td><a class='flickrimg' href=$photos_url$photo[id]>";
    $flickrbox .= "<img border='0' alt='$photo[title]' "
               ."src=" . $f->buildPhotoURL($photo, "Square") . ">";
    $flickrbox .= "</a></td>";
}

$flickrbox .= "</tr></table>";

You can see I’ve highlighted the altered sections in red. First, we up the $limit to 200. Then we introduce a new variable, $count. This variable will contain the number of photos we’d like displayed of the 200 we’re pulling. Next we change our foreach loop into a regular for loop. We will then randomly generate a number between 0 and 199 (remembering that arrays begin at 0, not 1). We run a while loop to check if that number is already in an array we’ve been keeping called $nonrepeatarray. If it is there, we draw another number. If it isn’t, we exit that loop, add it to the array, and keep moving with the portion of the loop dedicated to building the image display.

Although this may not be the most efficient method, it is simple. Plus, we have caching enabled through the phpFlickr API, so that will help. Other ways of doing the exact same thing include generating an array containing the values 0-199, shuffling the array, and then popping off one element each time. You are guaranteed not to have repeats.

For other methods of generating random, non-repeating numbers, with sample code, see here: http://www.phpbuilder.com/board/showthread.php?t=10329337.

Integrating Flickr with vBulletin

April 30th, 2009 by KenK

Our support forums here at iD Tech run on the popular internet forum software, vBulletin. Each year we constantly revamp the system with new upgrades, a new design, and new code plugins to make the process more specific to our business model and more accessible to our staff.

In addition, we also have a public Flickr stream. I wanted to pull these two elements together so that we could bring examples of our Flickr photos to our own support system. Here’s how I integrated them.

  1. Download the phpFlickr API from here: http://www.phpflickr.com/
  2. Unzip the files to your vBulletin /includes/ folder.
  3. If you want, you can rename the files to match the vBulletin naming convention (i.e. phpflickr.php -> functions_phpflickr.php).
  4. Optional: Create a new vBulletin product. From the admin side, choose Manage Products from the Plugins & Products menu. Then select Add/Import product. Fill in the information you need for a new product and hit save.
  5. Add a new vBulletin plugin. From the Plugins & Products admin menu, select Add New Plugin. Select the product you created in Step 4, or vBulletin otherwise. For hook location, you’ll need to select where you want the Flickr photos displayed. I’m placing them on our vBulletin homepage, so I select forumhome_start for my hook. Enter a title (such as Flickr Integration), leave the exectution order alone, and use the PHP code shown below.
  6. From the Styles & Templates menu, select Style Manager. Pick your style and choose Edit Templates. Find FORUMHOME (or whichever style is applicable to the hook location you chose) and double-click on it.
  7. Find the location in the template where you want your photos displayed and enter $flickrbox, the name of the variable we are pulling from our PHP plugin.

Now let’s take a look at the code we’re placing in our plugin.

require_once("includes/phpFlickr.php");

// Create new phpFlickr object
$f = new phpFlickr("yourapikey");
$f->enableCache("db","mysql://user:password@server/database");

$limit = 11;

// Find the NSID of the username inputted via the form
$person = $f->people_findByUsername('iDTechCamp');

// Get the friendly URL of the user's photos
$photos_url = $f->urls_getUserPhotos($person['id']);

// Get the user's first X($limit) public photos
$photos = $f->people_getPublicPhotos($person['id'], NULL, NULL, $limit);
$flickrbox = "<table class='flickrbox'><tr>";

// Loop through the photos and output the html
foreach ((array)$photos['photos']['photo'] as $photo) {
    $flickrbox .= "<td><a class='flickrimg' href=$photos_url$photo[id]>";
    $flickrbox .= "<img border='0' alt='$photo[title]' "
               ."src=" . $f->buildPhotoURL($photo, "Square") . ">";
    $flickrbox .= "</a></td>";
}

$flickrbox .= "</tr></table>";

First, we require_once the phpflickr php api library. Alter this line if you choose to name your library differently. Next, we create a new phpFlickr object, $f, and supply our Flickr API key. Change this line to reflect your unique API key. Next, we optionally enable the cache available with this library by supplying our mySQL database details. If you don’t have your details, don’t worry, this step isn’t required.

We then set a limit for how many photos we want. I’ve chosen 11. We use people_findByUsername to get photos for our particular user, in this case, the company username iDTechCamp. The resulting statements will pull $limit number of photos from the public profile of our user. Lastly, we need to construct a variable that holds the output as HTML. Our variable here is $flickrbox and as you can see I’m generating a table with CSS class style named flickrbox. Each image in the table is table cell and contains a link to that particular image. We also set the image title to the photo title pulled from Flickr.

You can easily alter the PHP code here to produce tableless HTML or any other structure you like. I hope these instructions help you to integrate your board and Flickr! Email me or comment with any questions you might have.

Tech Pranks

April 21st, 2009 by KenK

Let’s talk about pranks. Specifically, pranks involving technology. If you access to some fairly common technology, you can prank like never before. Here are a few options:

Ghost Writing

This prank is simple. If you have a coworker nearby, find an extra USB keyboard (you could even use your own) and, depending on how far you are from your coworker’s computer, one or more USB extension cords. Plug your extension into their computer when they aren’t around and discreetly run the cord over to your computer where you plug in either your keyboard or an extra one. Alternatively, you can use an extra mouse instead.

The key to this prank is subtlety. Most operating systems allow multiple input devices, so you can simultaneously use two keyboards. As you go through the day, sporadically type a few characters here and there on your extension keyboard, especially when your coworker is working on a document, or email. You don’t want them to become overly suspicious, but instead think that they are having trouble typing. Just an extra letter here and there to start.

Other methods of subtle harrassment with this technique include randomly pressing the Windows key (or some other function-type key) or turning Capslock on occasionally. Again, don’t over do it or it will raise suspicion. If you opted for the mouse instead, small movements or the occasional right-click should do just fine.

Materials:

  • USB extension cord(s)
  • USB keyboard

Hidden Friend

This prank requires any sort of device that can occasionally make noise. The original prank began with a stopwatch we received from a vendor for free, though you can use any similar device that can be set to beep on a regular interval. In this case, I set the time incorrectly so that the hourly beep wouldn’t occur on the hour for further annoyance. Just hide the watch somewhere and wait for the fun begins.
If you need a more extreme version of this prank, check out this product from ThinkGeek: http://www.thinkgeek.com/gadgets/electronic/8c52/. This version is set to random periods between beeps and designed just for this prank.

Materials:

  • Stopwatch or other small device that can beep routinely
  • ThinkGeek Annoy-A-Tron

Auricular Feedback

All you need for this prank is a stereo line-in cable. That’s the cable you use to connect your speakers to your computer. More specifically, it’s a cable with a 1/8in male headphone jack on each side. Most speakers come with one or several of these cables. Attach one end to the line-in or microphone jack on your coworker’s computer. These jacks usually have icons to represent what they are. Next, plug the other end of the cable into the speaker-out jack on your computer. Basically, the sound from your computer will be played on their computer as microphone or line-in input.
You may need to unmute or adjust the volume of their microphone/line-in while they are away. To start, set your volume very low. You don’t want to make them suspicious too soon. Make sure that no unexpected noise plays on your computer – it blows your cover quickly when your favorite mp3 starts playing on their computer. Instead, play small annoying sounds, like insect buzzes, audio tests (http://www.bbc.co.uk/wiltshire/audio/mosquito_sound.mp3, the Mosquito, a sound file with frequency that only certain ages can hear), or weird beeps and boops from Flash game sites with sample sounds. Don’t overdo it, and gradually increase either the volume or the obnoxiousness of the sounds as the day progresses for maximum effect.

Materials:

  • Speaker Line-In cable

Those are just a few for now, but check back in later for more!

Populating a Combo-box in PHP Dynamically from MySQL

March 31st, 2009 by KenK

For this article, let’s pretend you have the following database table. There are two columns (I’m separating the fields with commas for readability), Item and Price:

Cheese Pizza, 1.00
Pepporoni Pizza, 1.50
Sausage Pizza, 1.50
Cheese Calzone, 1.50
Ham Calzone, 2.00

What if you want to populate a combo-box with your possible options? We can write php that generates a drop-down box that has five item choices in it, based on our table above. Here is the sample code:

<?php

  // Connect to the database
  mysql_connect("localhost", "user", "password") or die(mysql_error());
  mysql_select_db("name") or die(mysql_error());

  // Has the form been submitted?
  if (isset($_POST['item'])) {

    // The form has been submitted, query results
    $queryitem = "SELECT * FROM table WHERE item = '".$_POST['item']."';";

    // Successful query?
    if($result = mysql_query($queryitem))  {

      // More than 0 results returned?
      if($success = mysql_num_rows($result) > 0) {

        // For each result returned, display it
        while ($row = mysql_fetch_array($result)) echo $row[serial];
      }
      // Otherwise, no results, tell user
      else { echo "No results found."; }
    }
    // Error connecting? Tell user
    else { echo "Failed to connect to database."; }
  }
  // The form has NOT been submitted, so show the form instead of results
  else {

    // Create the form, post to the same file
    echo "<form method='post' action='example.php'>";

    // Form a query to populate the combo-box
    $queryitem = "SELECT DISTINCT item FROM table;";

    // Successful query?
    if($result = mysql_query($queryitem))  {

      // If there are results returned, prepare combo-box
      if($success = mysql_num_rows($result) > 0) {
        // Start combo-box
        echo "<select name='item'>\n";
        echo "<option>-- Select Item --</option>\n";

        // For each item in the results...
        while ($row = mysql_fetch_array($result))
          // Add a new option to the combo-box
          echo "<option value='$row[item]'>$row[item]</option>\n";

        // End the combo-box
        echo "</select>\n";
      }
      // No results found in the database
      else { echo "No results found."; }
    }
    // Error in the database
    else { echo "Failed to connect to database."; }

    // Add a submit button to the form
    echo "<input type='submit' value='Submit' /></form>";
  }
?>