如何使用curl进行验证码生成和验证

来源:互联网 发布:淘宝客一单能赚多少 编辑:程序博客网 时间:2024/05/18 16:55


 

有时候我们觉得自己生成一个验证码很麻烦,看到别人做的一个验证码又觉得做得相当不错,自己想要在自己的网站上用上这个验证码,一时半会自己做不出来的情况下是不是就一点半法都没有了呢?不是的,这个时候我们可以通过curl从对方的服务器上取得验证码而验证的工作也在对方的服务器上进行,用别人的服务器为自己服务。下面就是相应的代码

 

a.php

 

   <!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html xmlns="http://www.w3.org/1999/xhtml">

   <head>

   <meta http-equiv="Content-Type" c />

     <title>a</title>

     <meta name="generator" c />

     <meta name="author" c />

     <meta name="keywords" c />

     <meta name="description" c />

   </head>

   <body>

   <iframe id='img' src="i.php" scrolling="No"frameborder="0"></iframe>

   <form action="b.php" method="POST">

   <input type="text" name="verify_code" />

   <input type="submit" />

   </form>

   </body>

   </html>

 

 

 

i.php

 

   <?php

   session_start();

   $cookie_jar=tempnam("temp","webbeast");

   $_SESSION['cookie_jar']=$cookie_jar;

   $ch=curl_init();

   curl_setopt($ch,CURLOPT_URL,"http://www.webbeast.cn/include/seccode.php");

   curl_setopt($ch,CURLOPT_REFERER,"http://www.webbeast.cn/?action=show&id=26&page=1");

   curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_jar);

   curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_jar);

   curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);

   curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);

   if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'msie')===false)

    {

       header("content-type:image/jpg");

    }

   curl_exec($ch);

   curl_close($ch);

   ?>

 

 

 

 

b.php

 

   <?php

   session_start();

   ?>

   <!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html xmlns="http://www.w3.org/1999/xhtml">

   <head>

   <meta http-equiv="Content-Type" c />

     <title>b</title>

     <meta name="generator" c />

     <meta name="author" c />

     <meta name="keywords" c />

     <meta name="description" c />

   </head>

   <body>

   <?php

   if(empty($_SESSION['cookie_jar'])) exit();

   $cookie_jar=$_SESSION['cookie_jar'];

   $verify=$_POST['verify_code'];

   $ch=curl_init();

   curl_setopt($ch,CURLOPT_URL,"http://www.webbeast.cn/post.php");

   curl_setopt($ch,CURLOPT_REFERER,'http://www.webbeast.cn/?action=show&id=26&page=1');

   curl_setopt($ch,CURLOPT_POST,1);

   curl_setopt($ch,CURLOPT_POSTFIELDS,"articleid=26&formhash=2d47d0be&username=cat&password=&url=&content=oh,MyGOD!&clientcode=".$verify."&action=addcomment");

   curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_jar);

   curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_jar);

   curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);

   curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);

   curl_exec($ch);

   curl_close($ch);

   ?>

   </body>

</html>

 

原创粉丝点击