IT.COM

[Php] Captcha not working (New)

Spaceship Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
Hi all been doing my own captcha system and it seems to not want to work and i cannot figure out why.
Here is my captcha class:
PHP:
class captcha_pxb
{
    function generateRandom($length=6)
    {
        // first array = digits, second = lowercase, third uppercase
        $_rand_src = array(
        array(48,57),
        array(97,122),
        array(65,90)
         );
         
        $random_string = "";
        for($i=0;$i<$length;$i++)
        {
            $i1=rand(0,sizeof($_rand_src)-1);
            $random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
        }
        return $random_string;
    }
     
    function make_session()
    {
        $_SESSION['captcha'] = $this->generateRandom(3);
        $_SESSION['captcha_tmp'] = $this->generateRandom(3);  
    }
    
    // check the wanted string against the one entered
    function validate($wanted, $entered)
    {
        if ($wanted != $entered)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
The image file:

PHP:
session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// lets make the captcha image from the blank image
$im = imagecreatefromjpeg("../../images/captcha_pxb.jpg");

// get the random strings from the session
$rand1 = $_SESSION['captcha'];
$rand2 = $_SESSION['captcha_tmp'];

ImageString($im, 5, 2, 2, $rand1[0]." ".$rand1[1]." ".$rand1[2]." ", ImageColorAllocate($im, 0, 0, 0));
ImageString($im, 5, 2, 2, " ".$rand2[0]." ".$rand2[1]." ".$rand2[2], ImageColorAllocate($im, 255, 0, 0));
         
Header('Content-type: image/jpeg');
imagejpeg($im, NULL, 100);
ImageDestroy($im);

How i show it (this of course is not the whole file and yes i do start the session):
PHP:
require_once(PXB_ROOT . '/includes/captchas/captcha_pxb.php');
     
$captcha = new captcha_pxb();
$captcha->make_session();

$guest_captcha = eval($templating->load('register', 'captcha_pxb'));
It is as if the image file doesn't pick up the session, i tried including the file with most things commented about bar echoing $rand1 and 2 and it shows the correct two strings, but it won't show on the image, the image is just my blank image with no letters/numbers . It can't be my templating functions can it since sessions are a global?
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
session_start();

Has to be called in the file you're including the captcha image in as well.

---------- Post added at 07:13 AM ---------- Previous post was at 07:12 AM ----------

So, try:

PHP:
session_start();

require_once(PXB_ROOT . '/includes/captchas/captcha_pxb.php');

$captcha = new captcha_pxb();
$captcha->make_session();

$guest_captcha = eval($templating->load('register', 'captcha_pxb'));
 
0
•••
Yes I know that, that is only a small snippet I start the session of course.

Like I said I've tried simply including the image generating file and having it simply echo the rand strings and they show but they won't show up on the image when I show the file like I showed.
 
0
•••
http://www.secondversion.com/Untitled.php

Testing there, it works. Are you sure you've specified the correct path to the image specified in imagecreatefromjpeg() ? Also, what happens if you visit the image file directly? (if in the web root)
 
0
•••
That's the thing the image shows up fine but the letters\numbers dont. I tried adding in hardcoded letters to see if the imagestrings are working and they showed up fine, so somehow the image file cannot grab the session strings when displayed using the template system.

Going to the page itself will come up blank obviously as it won't have any information to use from the script that grabs it.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back