价格不是我们的优势,品质永远是我们的追求!
当前位置:首页行业快讯 > 乐信揭秘php手机接收短信验证码实现编程案例
乐信揭秘php手机接收短信验证码实现编程案例
来源:动力思维乐信  时间:2015-11-24 11:04

  电商网站B2c/B2B网站注册首先考虑的就是手机验证码短信获取,而电商网站开发的手机端App 注册验证还是需要手机短信验证码,今天乐信就给大家揭秘php如何快速实现手机获取短信验证码的编程实例,以下供各位交流学习使用。

  我们这里展示了如何编写PHP程序实现验证码功能:

  代码一:

  /*

  * Filename: authpage.php

  * Author: hutuworm

  * Date: 2003-04-28

  * @Copyleft hutuworm.org

  */

  srand((double)microtime()*1000000);

  //验证用户输入是否和验证码一致

  if(isset($HTTP_POST_VARS['authinput']))

  {

  if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0)

  echo "验证成功!";

  else

  echo "验证失败!";

  }

  //生成新的四位整数验证码

  while(($authnum=rand()%10000)<1000);

  ?>

  代码二:

  /*

  * Filename: authimg.php

  * Author: hutuworm

  * Date: 2003-04-28

  * @Copyleft hutuworm.org

  */

  //生成验证码图片

  Header("Content-type: image/PNG");

  srand((double)microtime()*1000000);

  $im = imagecreate(58,28);

  $black = ImageColorAllocate($im, 0,0,0);

  $white = ImageColorAllocate($im, 255,255,255);

  $gray = ImageColorAllocate($im, 200,200,200);

  imagefill($im,68,30,$gray);

  //将四位整数验证码绘入图片

  imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);

  for($i=0;$i<50;$i++) //加入干扰象素

  {

  imagesetpixel($im, rand()%70 , rand()%30 , $black);

  }

  ImagePNG($im);

  ImageDestroy($im);

  ?>

  本文程序在Apache 2.0.45 + PHP 4.3.1环境下运行通过。

  上文只是对验证码功能的一个简单实现,并没有考虑商用安全性问题。如果要增强安全性,将此功能投入商业应用,则可以通过以下几个步骤实现:

  1. 启用Session。

  2. authnum在authimg.php中生成,并计算md5sum,存入session。

  3. authpage.php将authinput计算md5sum后,与session中的authnum(md5sum)对比得出验证结果。

  超越PHP 注:作者使用了简单的代码实现了很酷的功能。不过在添加干扰像素时的效果不是太好,大家可以看一下雨声论坛登录时的效验码(https://ror.cn/perl/ut/user_login.cgi),偶把第二段代码稍改了一下,生成了与其类似的效果。

  修改后的代码如下:

  /*

  * Filename: authimg.php

  * Author: hutuworm

  * Date: 2003-04-28

  * @Copyleft hutuworm.org

  */

  //生成验证码图片

  Header("Content-type: image/PNG");

  srand((double)microtime()*1000000);

  $im = imagecreate(62,20);

  $black = ImageColorAllocate($im, 0,0,0);

  $white = ImageColorAllocate($im, 255,255,255);

  $gray = ImageColorAllocate($im, 200,200,200);

  imagefill($im,68,30,$gray);

  while(($authnum=rand()%100000)<10000);

  //将四位整数验证码绘入图片

  imagestring($im, 5, 10, 3, $authnum, $black);

  for($i=0;$i<200;$i++) //加入干扰象素

  {

  $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

  imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);

  }

  ImagePNG($im);

  ImageDestroy($im);

  ?>

  以上就是由乐信短信验证码平台提供的php手机获取短信验证的编程实例,您如果需要短信验证码或是短信接口sdk 、短信接口API开发服务,可以联系乐信短信验证码专家服务,详情访问:https://www.lx598.com。