如何使用PHP获取当前会话值?

时间:2011-01-24 10:06:04

标签: php captcha

我在我的表单中使用了验证码编码。以下是验证码创建的编码

<?php  
   session_start();  

    $string = '';  

   for ($i = 0; $i < 5; $i++)
     {  
       $string .= chr(rand(97, 122));  
     }  

  $_SESSION['rand_code'] = $string;  



  $dir = 'fonts/';  

  $image = imagecreatetruecolor(150,60) or die('Cannot Initialize new image ');  
  $black = imagecolorallocate($image, 0, 0, 0);  
  $color = imagecolorallocate($image, 200, 100, 90); 
  $white = imagecolorallocate($image, 255, 255, 255);  

  imagefilledrectangle($image,0,50,150,100,$black);  
  imagettftext ($image, 30, 0, 10, 40, $color, $dir."BauhausMedium.ttf", $_SESSION['rand_code']);  


  header("Content-type: image/jpeg");  
  imagejpeg($image);  

  imagedestroy($image);




?> 

在其他页面中,我使用img标签调用此代码。

但问题是,在该页面中我想要textbox.i中的验证码图像使用$ _session ['rand_code'];但我显示以前的会话值,这是以前的验证码图像值。 我想要该页面中的当前图像值?

2 个答案:

答案 0 :(得分:1)

您在加载表单后访问验证码,因为表单中的图像实际上会触发验证码。

您只应在提交表单后检查验证码。

答案 1 :(得分:1)

一种方法是创建随机代码并将其保存在具有img标记的页面的会话中。在创建图像的页面上只需阅读会话。