0

I have a captcha-generator php code, which gives the value string to the $_SESSION['captcha_string'] session variable.

PHP captcha-generator code:

<?php
    session_start();
    header ("Content-type: image/png");
    $dirPath="/opt/lampp/htdocs/WebSiteFolder/dfxCaptcha/";
    $font='/opt/lampp/htdocs/WebSiteFolder/DejaVuSerif-Bold.ttf';
    $imgWidth=200;
    $imgHeight=50;        
    global $image;
    $image = imagecreatetruecolor($imgWidth, $imgHeight) or die("Cannot initialize a new GD image stream.");
    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 255, 255, 255);    
    imagefilledrectangle($image, 0, 0, $imgWidth, $imgHeight, $background_color);    
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';    
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $word = "";    
    for ($i = 0; $i < 4; $i++) {
        $letter = $letters[rand(0, $len - 1)];         
        imagettftext($image, 15, 0, $i*50+25, 50, $text_color, $font, $letter);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;
    $images = glob($dirPath."*.png");
    foreach ($images as $image_to_delete) {
        @unlink($image_to_delete);
    }
    imagepng($image);
?>

Tentatively, when I load the page that run the captcha-generator, I try to print out (see the code below) the value of the $_SESSION['captcha_string'] variable, but it gives the value which was previously generated (ie: delays). For example I load the page and the captcha image shows "ABCDE". I reload the page and the captcha image shows "BCDEF", but the printed value is "ABCDE". After it I reload the page again, and the captcha image shows "CDEFG" and the printed value is "BCDEF".

HTML code:

<?php session_start(); ?>

<!DOCTYPE html>

.. some irrelevant code here ..

<img id="captchaimg" src="captcha_generator.php">
<div><?php echo $_SESSION['captcha_string'] ?></div>

How can I do it right?

UPDATE: how can I achieve that the image and the $_SESSION['captcha_string'] will be the proper pair at the same time? Actually I need to use the ACTUAL $_SESSION['captcha_string'] in a javascript function. How?

1 Answer 1

1

This part means your browser loads captcha_generator.php AFTER you send the page to it.

<img id="captchaimg" src="captcha_generator.php">
<div><?php echo $_SESSION['captcha_string'] ?></div>

You are generating a new Captcha for the image. You need to turn that around.

You could generate the new Captcha on page load and just return the previously generated one on the image request.


You could avoid this problem if you have the captcha_generator.php return the image content, then send it directly as data-src:

<img id="captchaimg" src="data:image/png;base64,<?php echo base64_encode(include captcha_generator.php) ?>">
Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand it exactly. Aren't the order of execution the same as the order of the code in HTML? First the captcha is generated and the session variable set, then "print out".
Problem is: No you are calling captcha_generator.php. The browser is, after you send the whole page and it loads the image.
So, the whole page is loading BEFORE the generator-php runs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.