douwen5584 2017-07-18 19:16
浏览 60

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

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条回答 默认 最新

  • dsrnwngq411594 2017-07-18 19:18
    关注

    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) ?>">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料