Ken Ibarra
Ken Ibarra

Reputation: 21

session variable only retrieves the previous value

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($img, 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)

Upvotes: 2

Views: 1181

Answers (1)

nerdzila
nerdzila

Reputation: 126

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.

Upvotes: 3

Related Questions