Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2007, 06:06 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default PHP Captcha Script not rendering images - broken

I have a PHP Captcha script which I have been using from:

http://www.white-hat-web-design.co.u...hp-captcha.php

However, after following all the instructions, copying the file into C:\Windows\Fonts to install and having php_gd2.dll inside the /php/ext dir it still does not render an image.

The simple form.php script:

Code:
<?php 
session_start();

if( isset($_POST['submit'])) {
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
        // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
        echo 'Thank you. Your message said "'.$_POST['message'].'"';
        unset($_SESSION['security_code']);
   } else {
        // Insert your code for showing an error message here
        echo 'Sorry, you have provided an invalid security code';
   }
} else {
?>

    <form action="form.php" method="post">
        <label for="name">Name: </label><input type="text" name="name" id="name" /><br />
        <label for="email">Email: </label><input type="text" name="email" id="email" /><br />
        <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br />
        <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
        <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
        <input type="submit" name="submit" value="Submit" />
    </form>

<?php
    }
?>
CaptchaSecurityImages.php (script that generates the Captcha):
Code:
<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.u...hp-captcha.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

    var $font = 'monofont.ttf';

    function generateCode($characters) {
        /* list all possible characters, similar looking characters and vowels have been removed */
        $possible = '23456789bcdfghjkmnpqrstvwxyz';
        $code = '';
        $i = 0;
        while ($i < $characters) { 
            $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
            $i++;
        }
        return $code;
    }

    function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
        $code = $this->generateCode($characters);
        /* font size will be 75% of the image height */
        $font_size = $height * 0.75;
        $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);
        $text_color = imagecolorallocate($image, 20, 40, 100);
        $noise_color = imagecolorallocate($image, 100, 120, 180);
        /* generate random dots in background */
        for( $i=0; $i<($width*$height)/3; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
        }
        /* generate random lines in background */
        for( $i=0; $i<($width*$height)/150; $i++ ) {
            imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
        }
        /* create textbox and add text */
        $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
        $x = ($width - $textbox[4])/2;
        $y = ($height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
        /* output captcha image to browser */
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
        $_SESSION['security_code'] = $code;
    }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

echo "width: $width<br>";

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>
Any ideas with this one? I have php_gd2.dll commented back in in my php.ini file - what else do I need to do?

Picco

www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old October 5th, 2007, 02:15 AM
Authorized User
 
Join Date: Sep 2007
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

I assume you restarted the server after modifying the php.ini file

But also did you try putting that font in the folder where the php script resides (in the code it looks like it would call it from this directory) I don't know how it would know to grab it from c:\windows\fonts

But again just making a quick guess without much thinking....let me know if it still doesn't work and we can think of something else!

http://mynameissteve.com
 
Old October 13th, 2007, 07:45 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Yeah, i always restart Apache after changing php.ini

Also copied the font to the windows/font dir

I am now using ReCaptcha. Easy to implement and looks better to:

http://recaptcha.net/

Merci!

Picco



www.crmpicco.co.uk
www.ie7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 Try It Out Pg. 460 Broken Images workib BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 September 19th, 2008 10:29 AM
How to create Captcha using VB Script compad Classic ASP Basics 2 September 12th, 2007 03:00 AM
Rendering base64 images from XML in XSLT jilly XSLT 7 June 29th, 2007 07:56 AM
Rendering images with XSLT trufla XSLT 1 May 21st, 2006 11:37 AM
Call and run CGI script from a PHP script ... how? dbruins BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 10th, 2003 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.