PHP
  Home arrow PHP arrow Page 2 - Security Images in PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Security Images in PHP
By: Nathan Rohler
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 104
    2004-09-08

    Table of Contents:
  • Security Images in PHP
  • Getting Started -- Securityimage.php
  • Set Other Text Variables
  • Code for securityimage.php
  • Creating the Sign-up Demo -- Signupdemo.php
  • Form Handler Script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Security Images in PHP - Getting Started -- Securityimage.php


    (Page 2 of 6 )

    The first page to create is the page which will generate the security images, securityimage.php. First, we need to open the PHP block and determine if we can use refid in the querystring; otherwise, we generate a unique one ourselves.

    <?php
    //Generate Reference ID
    if (isset($HTTP_GET_VARS["refid"]) && $HTTP_GET_VARS["refid"]!="") {
       $referenceid = stripslashes($HTTP_GET_VARS["refid"]);
    } else {
       $referenceid = md5(mktime()*rand());
    }

    Now, we must select a font to use. For this demo, we will use Century, because it is easy to distinguish between zeroes and o's, and because it is a serif font, it is harder for a computer to comprehend. You will need to set the path to the font, reflecting what OS you are using and where the fonts folder is.

    //Select Font
    $font = "C:\WINDOWS\Fonts\Century.ttf";

    Next, we will randomly select one of the three backgrounds (from the included files). Using one of these files, we initialize a new image identifier $im, representing the background image, using ImageCreateFromPNG. If you want more variety, you can create more backgrounds (must be PNG format).

    //Select random background image
    $bgurl = rand(1, 3);
    $im = ImageCreateFromPNG("images/bg".$bgurl.".png");

    After this, we will generate a random string. To do this, we first create an array of characters to use. Then, we specify a length for the text to be generated. For this demonstration, we will use 8, but you can change it to any length you wish. Finally, we create an empty variable, then randomly fill it with characters in a loop.

    //Generate the random string
    $chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G",
    "h","H","i","I","j","J","k",
    "K","l","L","m","M","n","N","o","O","p","P","q","Q","r",
    "R","s","S","t","T","u","U","v",
    "V","w","W","x","X","y","Y","z","Z","1","2","3","4","5",
    "6","7","8","9");
    $length = 8;
    $textstr = "";
    for ($i=0; $i<$length; $i++) {
       $textstr .= $chars[rand(0, count($chars)-1)];
    }

    More PHP Articles
    More By Nathan Rohler


       · Good article, I would cut out the hidden form fields and database and use session...
       · I think the author needs to upgrade their version of php, because $HTTP_GET_VARS is...
       · maybe it would be better not to store $hiddentext in mysql, but its...
       · Very good article[url=http://www.virtual-interconnect.co.uk/]Visit us[/url]
       · Seeing PHP3 code when PHP5 is current is a bit disappointing. The author should...
       · ...need to check random word through a list of profane words so you don't get the...
       · May not be supported, but it's still in use in a lot of places.
       · Wouldn't it even be possible to store an md5-hash in the form itself? That way there...
       · I get this error: "Fatal error: Call to undefined function: imagecreatefrompng() in...
       · hi, i think changing the option from png to jpeg will work fine . though some of the...
       · in function checkSecurityImage, after veirfying that row count is more then zero,...
       · Only if the hash mix the string and some other text known only by the php...
       · Great article! Helped me accomplish my task very quickly. Thanks for writing this...
       · This is a great article! I had some issues though with the implementation of...
       · I Think this article can be strengthened by the fuction imagecopyresized() where if...
       · The problem with the database method used in this example is that a bot could fill...
       · Only site visitors with cookies enabled would be able to use it.
       · Nice article. I'm having one problem though, there's no text on the generated image....
       · You can always have a small font.ttf and host the file in the same folder. but try a...
       · When you don't see the text, you have to change length in $length (for ($i=0;...
       · Do the support files work? I noticed things like form tags missing etc...When I...
       · Or just remove the vowels to avoid spelling any english word
       · It says that there are some files included, but they're not.. Handy handy
       · Hi,I am unable to get this to work - has anyone ahd any luck wih it. The data...
       · I'm not a php programmer.I'm carpenter.This article helped me best to put...
       · Since I'm a beginner to php, it's my first time come into GD library.This article...
       · This way gives you 24 hours to post, nice for people like me who leave the page open...
       · I dont what happen but my text in my image security is not there..why?tell me?
       · I am beginner PHP programmer. I like your article. From your article I got better...
     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT