Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Birmingham (UK)
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    CAPTCHA issue setting session variable

    I have a form with a CAPTCHA image.

    The image is is displayed fine in the browser with the following HTML but I need to set a session variable set to the characters generated in the image. The session variable is set in the php file CaptchasecurityImages.php

    HTML CODE
    <td align="right"><p><img src="CaptchaSecurityImages.php?width=100&height=40&characters=6" /></p> </td>

    The line $_SESSION['security_code'] = $code; (near the end of the php below) does not set the session variable. Any help or guidance would be much appreciated - Many thanks Dambuster

    THE PHP CODE CaptchaSecurityImages.php is:
    <?php
    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';
    $captcha = new CaptchaSecurityImages($width,$height,$characters);

    ?>

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,206
    Thanks
    10
    Thanked 1,166 Times in 1,157 Posts
    The very top of EVERY script that uses SESSION ... should look like this ...

    <?php
    session_start();


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •