Php 如何在正确的时间以编程方式发送$u POST请求

Php 如何在正确的时间以编程方式发送$u POST请求,php,post,Php,Post,为了在我的注册表上实现验证码,我有两个文件captcha_image.php和captcha_api.php,它们的内容如下所示。 在下面的代码中,虽然数据确实被发送到了captcha_image.php,但是发送得太快,并且当我再次通过src属性调用captcha_image.php时,$\u POST数组已经被清空。我怎样才能解决这个问题 captcha_image.php的内容: <?php //Set the image width and height $wid

为了在我的注册表上实现验证码,我有两个文件captcha_image.php和captcha_api.php,它们的内容如下所示。 在下面的代码中,虽然数据确实被发送到了captcha_image.php,但是发送得太快,并且当我再次通过src属性调用captcha_image.php时,$\u 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>';

?>

你做错了

只需使用$\会话保存安全代码

  • 生成图像和生成安全代码的脚本。它将安全代码保存到会话,并输出到映像

  • 检查验证码时,只需比较用户输入表单和会话中的值


  • 尝试添加一个隐藏的输入字段,以便携带验证码数据进行检查。

    我花了一些时间才理解您的答案,但您完全正确。我所需要做的就是根本不使用$\u POST,只使用$\u会话。