PHP
  Home arrow PHP arrow Page 4 - Security Images in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Security Images in PHP
By: Nathan Rohler
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 109
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Code for securityimage.php
    ( Page 4 of 6 )

    The complete code listing for securityimage.php should now look like this:

    <?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());
    }

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

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

    //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)];
    }

    //Create random size, angle, and dark color
    $size = rand(12, 16);
    $angle = rand(-5, 5);
    $color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100));

    //Determine text size, and use dimensions to generate x & y coordinates
    $textsize = imagettfbbox($size, $angle, $font, $textstr);
    $twidth = abs($textsize[2]-$textsize[0]);
    $theight = abs($textsize[5]-$textsize[3]);
    $x = (imagesx($im)/2)-($twidth/2)+(rand(-20, 20));
    $y = (imagesy($im))-($theight/2);

    //Add text to image
    ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr);

    //Output PNG Image
    header("Content-Type: image/png");
    ImagePNG($im);

    //Destroy the image to free memory
    imagedestroy($im);

    //Insert reference into database, and delete any old ones
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("dw_php");
    //Create reference
    mysql_query("INSERT INTO security_images (insertdate, referenceid, hiddentext) VALUES (
    now(), '".$referenceid."', '".$textstr."')");
    //Delete references older than 1 day
    mysql_query("DELETE FROM security_images
    WHERE insertdate < date_sub(now(), interval 1 day)");

    //End Output
    exit;

    ?>

    Open securityimage.php in a browser. You should see something like the image below:

    Security Images in PHP

    Now, we are finished with the file and ready to put it to use!

    Note: If you receive a message about ImageCreateFromPNG being an undefined function, this is probably because you don't have the GD Graphics Library installed. Install this (download may be obtained from http://www.boutell.com/gd/) and retry.



     
     
    >>> More PHP Articles          >>> More By Nathan Rohler
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek