Email Code

  Home arrow Email Code arrow Random validation image

Random validation image
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 15

    Table of Contents:



    Use this script in your contact form, for you whois query tool or just there where some extra validation is needed. A session will be created inside a dynamic image file (requires GD library). The random value of this image appears inside the generated image. The user has to enter this value into formfield. This value will be checked while processing the form. Without entering this value a form will not be processed.

    By : olaf

    <?php /*
    example of usage:

    inside your form
    <input type="text" name="validator" id="validator" size="4" />
    <img src="random.php" alt="" width="60" height="20" vspace="1" align="top" />

    and test the value of the "validator" form field like:
    if (!empty($_POST['validator']) && $_POST['validator'] == $_SESSION['rand_code']) {
    process your form here
    at least destroy the session

    // save this code in your random script

    if (empty($_SESSION['rand_code'])) {
    $str = "";
    $length = 0;
    for ($i = 0; $i < 4; $i++) {
    // this numbers refer to numbers of the ascii table (small-caps)
    $str .= chr(rand(97, 122));
    $_SESSION['rand_code'] = $str;

    $imgX = 60;
    $imgY = 20;
    $image = imagecreatetruecolor(60, 20);

    $backgr_col = imagecolorallocate($image, 238,239,239);
    $border_col = imagecolorallocate($image, 208,208,208);
    $text_col = imagecolorallocate($image, 46,60,31);

    imagefilledrectangle($image, 0, 0, 60, 20, $backgr_col);
    imagerectangle($image, 0, 0, 59, 19, $border_col);

    $font = "VeraSe.ttf"; // it's a Bitstream font check for more
    $font_size = 10;
    $angle = 0;
    $box = imagettfbbox($font_size, $angle, $font, $_SESSION['rand_code']);
    $x = (int)($imgX - $box[4]) / 2;
    $y = (int)($imgY - $box[5]) / 2;
    imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $_SESSION['rand_code']);

    header("Content-type: image/png");
    imagedestroy ($image);
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Email Code Articles
    More By Codewalkers

    blog comments powered by Disqus


    - Basic Ajax contact form
    - Random validation image
    - tinySendMail
    - SaferMail 0.7
    - Smtp Auth Email Script
    - Search Mime Email Structure
    - PHP Text / HTML Email with Attachments 2.1
    - Simple way to send mail wih one attached Doc...
    - Generic POP3 Class
    - PHP Text / HTML Email with Unlimited Attachm...
    - Another SendMail
    - email with attachment
    - Get Email Addresses from Strings
    - EmailCode
    - Email Validation - Gone Wild

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap