无法获取实际会话变量(HTML/PHP)

无法获取实际会话变量(HTML/PHP),php,html,session,Php,Html,Session,我有一个验证码生成器php代码,它将值字符串提供给$\u会话['captcha\u string']会话变量 PHP验证码生成器代码: <?php session_start(); header ("Content-type: image/png"); $dirPath="/opt/lampp/htdocs/WebSiteFolder/dfxCaptcha/"; $font='/opt/lampp/htdocs/WebSiteFolder/DejaVuSer

我有一个验证码生成器php代码,它将值字符串提供给
$\u会话['captcha\u string']
会话变量

PHP验证码生成器代码:

<?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);
?>
我怎样才能把它做好


更新:如何使图像和
$\u会话['captcha\u string']
同时成为正确的一对?实际上,我需要在javascript函数中使用实际的
$\u会话['captcha\u string']
。如何加载?

这部分表示在您将页面发送到浏览器后,浏览器将加载
验证码生成器.php

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

我不太明白。执行顺序与HTML中的代码顺序不一样吗?首先生成captcha并设置会话变量,然后“打印输出”。问题是:不,您正在调用
captcha\u generator.php
。浏览器是,在发送整个页面并加载图像之后。那么,在生成器php运行之前加载整个页面?
<img id="captchaimg" src="captcha_generator.php">
<div><?php echo $_SESSION['captcha_string'] ?></div>
<img id="captchaimg" src="data:image/png;base64,<?php echo base64_encode(include captcha_generator.php) ?>">