Jan 18 2010

Simple Password Generator

In this post I will show you how to create a simple, secure, password generator with PHP. The generated password will have special characters, letters and numbers mixed up.

The code:

1
2
3
4
5
6
7
8
$length = 10; # specify the length of the password
$randomPw = ''; #variable to store the password

# Generate each character
for ($i=0; $i < $length; $i++) {
 # I will explain this line at the bottom of this post
 $randomPw .= chr(rand(33,125));
}

At line 7 you see  some nested functions, two actually: chr() and rand(). The chr(int val)-function will generate a number too an ASCII value from val and the rand(int max,int min) will generate a random number between max and min. So I think you can guess what 33 and 125 stands for… Indeed, ASII-values.

So we generate a number between ASCII-value 33 and 125, which represent special characters, letters and numbers, with rand(33,125). The chr(rand(33,125)) converts this number too his character, letter or number and .= adds the value to the string randomPw.


Dec 7 2009

At Work

It has been a while since my last post. Some major changes happend since then. I got a job!! I work as a Data Manager in Mechelen in a company called SGS Life Science Services. We are a CRO and we do clinical research.

So from now on I have to get up at 05.30AM to ahead of the traffic jams and arrive on time around 07.10AM. And leave 8 hours later (incl lunch time) at 04.00PM, again before the highways get crowed!

I officially started the 1st December 2009 and I received a company car, a VW Polo. I have got some training regarding the programs they use (TOAD) and the database structure (SDTM).

Hopefully I can write more exciting stuff in the next few weeks!


Oct 27 2009

Zend_Captcha_Image Tutorial

I was working on a project recently and I wanted to use Captcha. I coded my project in a Zend Framework environment so it was obvious to take a look at the Zend_Captcha- class, more specifically Zend_Captcha_Image. In this tutorial I’ll explain how to use and implement the Zend_Captcha_Image-class in your project (back-and front-end).

Here is a list of what need to do to get our instance of Zend_Captcha_Image up and running:

  1. Make sure GD is installed
  2. Make a new folder in you web dir with 777 permissions, this will store the captcha images
  3. Write the generateCaptcha()-function
  4. Write the validateCaptcha()-function
  5. Implement the Captcha in your front-end and stop the fearing robot

Make sure GD is installed

Make sure you have GD up and running. To check if GD is running in your copy of PHP run the following code on any PHP page to finde out.

1
2
3
4
5
if( function_exists("gd_info") ) {
  echo "My GD is up and running";
} else {
  echo "My GD is not installed";
}

This should explain itself: if the message “My GD is not installed” appears you should contact your provider for more info.

Make Captcha images folder

So GD is installed.. We are almost good to go! First we need to make a folder to store you Captcha images in, do this within you /public – directory and chmod it to 777. Lets generate now the two functions that will serve as the base.

Write generateCaptcha()-function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* Generates an instance of Zend_Captcha
* Returns ID of captcha session
*/
function generateCaptcha() {
  $captcha = new Zend_Captcha_Image();
  $captcha-&gt;setTimeout("300")
              -&gt;setWordLen("6")
              -&gt;setHeight("80")
              -&gt;setFont("/path/to/your/fontFile.ttf")
              -&gt;setImgDir("./path/to/your/image/captchaDirectory");  // Do not forget the '.' at the beginning of the path, this is necessary to make this function work!
 
  $captcha-&gt;generate();    //command to generate session + create image
 
  return $captcha-&gt;getId();   //returns the ID given to session &amp; image
 
}

Let me explain, first we create a new object of our class Zend_Captcha_Image. We used it functions to feed it with some basic information:

  • ->setTimeout(”300″): The time the session will stay active in seconds, se the user has 5 minutes to respond.
  • ->setWordLen(”6″): Obviously this will represent the length of the Captcha word.
  • ->setHeight(”80″): Sets the height of the image.
  • ->setFont(”/path/to/your/fontFile.ttf”): points to which font you want to use in your images. It is necessary that you upload and use a font.
  • ->setImgDir(”./path/to/your/image/captchaDir”): This is the path where you Captcha images will be stored. Notice the ‘.‘ at the beginning of the path. Without this point the script will not work
    Explanation from the Zend manual:
    “setImgDir($imgDir) and getImgDir() allow you to specify the directory in which captcha images are stored. This defaults to “./images/captcha/”, which should look relative to the bootstrap script.”

Write validateCaptcha()-function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Validates captcha response
function validateCaptcha($captcha) {
                $captchaId = $captcha["id"];
                $captchaInput = $captcha["input"];
                $captchaSession = new Zend_Session_Namespace("Zend_Form_Captcha_" . $captchaId);
                $captchaIterator = $captchaSession-&gt;getIterator();
                $captchaWord = $captchaIterator["word"];
                if( $catchaWord ) {
                     if( $captchaInput != $captchaWord ){
                          return false;
                     } else {
                          return true;
                     }
                } else {
                     return false;
                }
}

This function is pretty simpel as well. First, you store the data from the form and sent it trough the function in $captcha, an array you setup in the controller later on. it contains two pieces of data:

  1. the Captcha session’s id
  2. what the user entered in your form

Using the id it accesses the session and gets the correct word.

Front- and back-end implementation

Place the following in your back-end

1
2
3
4
5
6
7
8
9
10
11
// Setup Captcha
$captchaId = generateCaptcha();        // Generates Captcha image and session, returns session’s ID
 
if( isset($_POST["captcha"]) ) {       // If a post value is sent, process form
  $captcha = $_POST["captcha"];     // Get array sent in $_POST form
  if( validateCaptcha($captcha) ) {
      // Here will the user, probably, be human.
  } else {
     // Here will the user be a bot or a typo occurred.
  }
}

And place the following in your front-end

1
2
3
4
5
6
7
8
<form method="POST">
  <img src="/your/captchaDirectory/&lt;?=$captchaId ?&gt;.png" alt="Captcha code" />
 
  What’s the word above say?
<input name="captcha[input]" type="text" />
<input name="captcha[id]" type="hidden" value="&lt;?=$captchaId ?&gt;" />
<input type="submit" value="Test" />
</form>

I’m sure you guys can figure it our where to place this code! This code works fine for me, hopefully it will do the same for you!


Oct 16 2009

Make Filesharing More Personal

I used to share my files (screenshots, PDF’s) with Quickshareit instead of transfer my file over MSN.  But I discover Fileshuttle, Fileshuttle does the same trick but uses the magic in a different way. Quickshareit uploads your file to it own server and obviously uses its own domain name as URL. Fileshuttle will let you use the benefits of the FTP-protocol and will upload the file to your own server. So, instead of a quickshare url you will have your own domain name as a reference — www.mydomainname.com/shares. Or you could make it readable: shares.mydomainname.com.


Oct 15 2009

Famous Swing Comic

Every individual in the IT-bussiness should know the famous swing comic. It’s a realistic reflection of the real deal: how a product is manufactured from beginning to end.

Full size

Full size