PHP Captcha – Session from generated image ist not stored

I have a form that generated a captcha, which of course will be checked afterwards. This also worked great in my test environment.

Now i have integrated the form in a onlineshop and it doesn’t work. The image for the captcha is generated. But apparently the security code whoich is generated in captcha.ph is not saved in the SESSION.

This is the code I use to insert the image:

<?
session_start();
?>
<img src="/media/content/captcha.php?RELOAD=" alt="Captcha" title="Klicken, um das Captcha neu zu laden" onclick="this.src+=1;document.getElementById('captcha_code').value='';" width="130px" height="38px" />
<input type="text" name="captcha_code" id="captcha_code" class="form-control"/>

<?
echo 'Test: '.$_SESSION['captcha_spam'].'';
?>

This is the Code from the capcha.php

<?
#Ersetellt eine Session, wenn dies noch nicht passiert ist.
session_start();
#Setzt das bestehende Captcha zurück.
unset($_SESSION['captcha_spam']);

#Allgemeine Variablen für das Captcha-Bild
$captcha_bg_img     = 'https://akkurato.de/media/content/etl-formular/bg_captcha.png';          #Pfad zum Hintergrundbild
$captcha_over_img   = 'https://akkurato.de/media/content/etl-formular/bg_captcha_over.png'; #Pfad zum Bild, was über das Captcha gelegt wird
$font_file = $_SERVER["DOCUMENT_ROOT"].'/public/fonts/KFOlCnqEu92Fr1MmSU5fBBc9.ttf';
#$font_file             = 'https://akkurato.de/public/fonts/KFOlCnqEu92Fr1MmSU5fBBc9.ttf';
#$font_file             = dirname(__FILE__).'/railway-webfont.ttf'; #Pfad zur Schriftdatei
$font_size          = 25;                                       #Schriftgröße
$text_angle         = mt_rand(0, 5);                            #Schriftwinkel (Werte zwischen 0 und 5)
$text_x             = mt_rand(0, 18);                           #X-Position (Werte zwischen 0 und 18)
$text_y             = 35;                                       #Y-Position
$text_chars         = 5;                                        #Länge des Textes
$text_color         = array(0, 0 , 0);                          #Textfarbe (R, G, B)
    
#Generiert einen zufälligen String.
#Verwendet keine 0, 1, I, O da sich diese ähnlich sehen.
function rand_string($length=5) {
    $letters = array_merge(range('A', 'H'), range('J', 'N'), range('P', 'Z'), range(2, 9));
    $lettersCount = count($letters) - 1;        
    $result = '';
        for ($i = 0; $i < $length; $i++) {
        $result .= $letters[mt_rand(0, $lettersCount)];
        }
    return $result;
}

#Generiert einen zufälligen Text.
$text = rand_string($text_chars);
$_SESSION['captcha_spam'] = $text;
    
#Header: Mitteilen, dass es sich um ein Bild handelt und dass dieses nicht im Cache gespeichert werden soll.
header('Expires: Mon, 26 Jul 1990 05:00:00 GMT');
header("Last-Modified: ".date("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');
header('Content-type: image/png');
    
#Captcha Bild erstellen, Text schreiben & Bild darüber legen.
$img = ImageCreateFromPNG($captcha_bg_img);
$text_color = ImageColorAllocate($img, $text_color[0], $text_color[1], $text_color[2]);
imagettftext($img, $font_size, $text_angle, $text_x, $text_y, $text_color, $font_file, $text);
imagecopy($img, ImageCreateFromPNG($captcha_over_img), 0, 0, 0, 0, 140, 40);
    
#Ausgabe und Löschen des Bildes.
imagepng($img); 
imagedestroy($img); 
?>

Both files are in the same directory.

What can be the reason that I can’t print the content of the session? All other sessions where print. This Code worked without any problems on my test system. It is now integrated into a shop system (not as an iFrame!), could it be due to a cache system or something?

I had inserted the file with and without the URL (https:://), but there was no difference either.

I used the search function and couldn’t find any solution.