如何在合适的时间以编程方式发送$ POST请求

时间:2015-02-19 06:00:55

标签: php post

要在我的注册表单上实现验证码,我有两个文件captcha_image.php和captcha_api.php,它们的内容如下所示。   看起来在下面的代码中,尽管数据确实被发送到captcha_image.php,但它发送得太快了,当我通过src属性再次调用captcha_image.php时,$ _POST数组已经被清空了。我该如何解决这个问题?

captcha_image.php的内容:

<?php

    //Set the image width and height
    $width = 100;
    $height = 20; 

    //Create the image resource 
    $image = ImageCreate($width, $height);  

    //We are making three colors, white, black and gray
    $white = ImageColorAllocate($image, 255, 255, 255);
    $black = ImageColorAllocate($image, 0, 0, 0);
    $grey = ImageColorAllocate($image, 204, 204, 204);

    //Make the background black 
    ImageFill($image, 0, 0, $black); 


    //Add randomly generated string in white to the image
    $security_code=isset($_POST['a'])?$_POST['a']:'default';
    ImageString($image, 3, 30, 3, $security_code, $white); 

    //Throw in some lines to make it a little bit harder for any bots to break 
    ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
    imageline($image, 0, $height/3, $width, $height/3, $grey); 
    imageline($image, $width/3, 0, $width/2, $height, $grey); 
    imageline($image, 2*$width/3, 0, $width/2, $height, $grey); 

    //Tell the browser what kind of file is come in 
    header("Content-Type: image/jpeg"); 

    //Output the newly created image in jpeg format 
    ImageJpeg($image);

    //Free up resources
    ImageDestroy($image);

    exit();


?>

captcha_api.php的内容:

<?php

  function do_post_request($url, $data, $optional_headers = null)
{
  $http_data=http_build_query($data);
  $params = array('http' => array(
              'method' => 'POST',
              'header'=>"Content-Type: application/x-www-form-urlencoded",
              'content' => $http_data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}


   //Let's generate a totally random string using md5
  $md5_hash = md5(rand(0,999)); 
  //We don't need a 32 character long string so we trim it down to 5 
  $security_code = substr($md5_hash, 15, 5); 

        //Set the session to store the security code
        $_SESSION["security_code"] = $security_code;
        $short_url1=dirname(__FILE__);
        $short_url2=substr($short_url1,strlen($_SERVER['DOCUMENT_ROOT']));
        $url='http://'.($_SERVER['HTTP_HOST']).$short_url2.'/captcha_image.php';
        $data=array('a'=>$security_code);
     do_post_request($url, $data,NULL);

echo '<fieldset> <legend>Security question :</legend> '.
' Copy what you read on the right
  <table>
  <tr>
  <td>
  <input id="Captcha_id" type="text" name="txtCaptcha" 
  value="" maxlength="10" size="32" />
  </td>
  <td>
  <img id="imgCaptcha" src="captcha_image.php" />
  </td>
  </tr>
  </table>
  </fieldset>';

?>

2 个答案:

答案 0 :(得分:1)

你做错了。

只需使用$ _SESSION来保存安全码。

  1. 生成图像并生成安全代码的脚本。它将安全代码保存到会话和图像中。

  2. 检查验证码时 - 只需比较用户输入表单和会话中的值。

答案 1 :(得分:0)

尝试添加隐藏的输入字段以携带要检查的验证码数据。