开发者

session variable only retrieves the previous value

开发者 https://www.devze.com 2023-03-31 10:03 出处:网络
I need to get a session variable value and compare it with an input. here is the code: <?php echo \"<img src=\'captchas.php\'><br><center><input type=\'text\' size=\'10\' nam

I need to get a session variable value and compare it with an input.

here is the code:

<?php
echo "<img src='captchas.php'><br><center><input type='text' size='10' name='check' id='postcaptcha' onChange='validatecaptcha();'></center><div id='validationforcaptcha'></div>";

$captchacode = $_SESSION['newcode'];

echo "<input type='text' name='sessioncaptcha' id='sessioncaptcha' value=$captchacode />"; 
?>

and here is the captcha.php

    <?php session_start(); ob_start();

$img = imagecreatefrompng('IMAGES/black.png'); 
$numero = rand(100000000, 999999999); 
$_SESSION['check23'] = $numero;
$white = imagecolorallocate($img, 255, 255, 255); 
imagestring($i开发者_如何转开发mg, 10, 8, 3, $numero, $white); 

header ("Content-type: image/png"); 
imagepng($img); 

?>

i don't know why the $_SESSION['check23'] variable only retrieves the previous captcha code that has been displayed (not what is currently displayed)


Maybe you're assuming that your first script gets executed after captcha.php?

This is what's actually happening:

  1. Your first script is executed and the page is served to the user.
  2. The user's browser requests captcha.php as an image.
  3. captcha.php is executed and the image is sent to the user.

Try generating the CAPTCHA code in the first script and saving it to $_SESSION['check23']. You can retrieve $_SESSION['check23'] later in captcha.php to render the appropriate image.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号