xml地图|网站地图|网站标签 [设为首页] [加入收藏]

正规赌博平台

当前位置:网上十大正规赌博平台 > 正规赌博平台 > 用session与gd库实现简单验证码生成与验证的类方

用session与gd库实现简单验证码生成与验证的类方

来源:http://www.nb-machinery.com 作者:网上十大正规赌博平台 时间:2019-07-10 23:26

实例如下:

验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的。目前大大小小的网站都有验证码。今天自己实现了一个简单的验证码类。说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下。当然,因为字体的原因,要想一眼看出来并不容易。同时,为了避免字母的大小写与数字混淆,又去掉了那些看起来很像的字母数字。
类:

本文实例讲述了php版微信小店API二次开发及使用方法。分享给大家供大家参考,具体如下:

<!doctype html>
<meta http-equiv="content-type" content="text/html" charset="utf-8"/>
switch语句,switch语句用于根据多个不同条件执行不同动作。<br/>
如果你希望有选择地执行若干代码块之一,还请使用switch语句。
<br/>
语法结构如下:
<pre>
switch(n)
{
case label1:
  如n=label1,此处代码将执行;
  break;
case label2:
  如n=label2,此处代码将执行;
  break;
case label3:
  如n=label3,此处代码将执行;
  break;
default:
  如果n既不等于label1页不等于label2,此处代码将执行;
}
</pre>
<hr/>
我们写一个程序,一个单选框。用switch语句半段。
<br/>
程序如下:
<br/>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
红色:<input type="radio" name="color" value="1"/><br/>
蓝色:<input type="radio" name="color" value="2"/><br/>
绿色:<input type="radio" name="color" value="3"/><br/>
<input type="submit"/>
</form>
<?php
if(!empty($_GET['color'])){
switch($_GET['color'])
{
case 1:
 echo"你选择的是红色!"; 
 break;
case 2:
 echo"你选择的是蓝色!";
 break;
case 3:
 echo"你选择的是绿色!";
 break;
default:
 echo"你没有选择任何一种颜色!";
}
}
?>
<?php
/**
 *简单生成验证码类
 */
class Captcha {
  private $width;//验证码宽度
  private $height;//验证码高度
  private $countOfChars;//字符数
  //private $distrubLines;//干扰线条数
  private $chars;//随机生成的字符串

  public function __construct($width=100,$height=30,$countOfChars=4,$distrubLines=2) {
  //初始化参数
  $this->width=$width;
  $this->height=$height;
  $this->countOfChars=$countOfChars;
  session_start();
  }

  /**
   * 执行全部动作,生成验证码并直接输出
   */
  public function execute(){
  $imageHandle=$this->createImage();
  $this->createChars();
  $this->drawChars($imageHandle);
  $this->outImage($imageHandle);
  }

  /**
   * 创建画布,并随机填充颜色
   * @return 返回画布句柄
   */
  public function createImage(){
  $imageHandle= imagecreate($this->width, $this->height);
  //随机背景颜色
    $randColor=imagecolorallocate($imageHandle, 50, mt_rand(0, 50), mt_rand(0, 50));
  imagefill($imageHandle, 0, 0, $randColor);
  return $imageHandle;
  }

  /**
   * 生成随机字符
   */
  private function createChars(){
  //候选字符
  $str='ABCDEFGHJKLMNPQRSTUVWXZabcdefghijkmnpqtuvwx2346789';
  $chars='';
  for($i=0;$i<$this->countOfChars;$i  ){
    $chars.=$str[mt_rand(0,strlen($str)-1)];
  }
  $this->chars=$chars;
  //保存在会话中
  $_SESSION['captcha']=strtolower($chars);
  }

  /**
   * 将字符写入图像
   * @param type $imageHandle 图像句柄
   */
  private function drawChars($imageHandle){
  if($this->chars!=null){
    $font='/home/WWW/YuWeiLiShuFT.ttf';
    for($i=0;$i<strlen($this->chars);$i  ){
    $color= imagecolorallocate($imageHandle,mt_rand(50, 200),mt_rand(100, 255),255);
    imagefttext($imageHandle,30, 30,$i*20 10,25,$color,$font,$this->chars[$i]);
    }
  }
  }

  /**
   * 输出图像
   * @param type $imageHandle 图像句柄
   */
  private function outImage($imageHandle){
  imagepng($imageHandle);
  imagedestroy($imageHandle);
  }

  /**
   * 判断用户输入的验证码是否正确
   * @param type $usrInput 用户的输入
   * @return boolean 验证码是否匹配
   */
  public static function isRight($usrInput){
  if(isset($_SESSION['captcha'])){
    if(strtolower($usrInput)==$_SESSION['captcha']){
    $_SESSION['captcha']=null;
    return true;
    }else{
    $_SESSION['captcha']=null;
    return false;
    }
  }
  }
}
  1. weixiaodian.php页面:

以上就是小编为大家带来的利用switch语句进行多选一判断的实例代码全部内容了,希望大家多多支持脚本之家~

把验证设置成了静态方法,因为生成验证码后已经把验证码存到了session中,验证时直接调用静态方法,而不需要实例化这个类了。

<?php
  class wXd
  {
    public $AppID = "";
    public $AppSecret = "";
    public $OutPut = "";
    public $AccessToken = "";
    public $ID = "";
    public $HandleAT = array();
    public $Logistics = array();
    public function __construct($ID = '0'){
      $this->ID = $ID;
      $this->sLogisticsList();
    }
    public function cUrlRequest($url,$data = null){
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
      if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      }
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      $output = curl_exec($curl);
      curl_close($curl);
      return $output;
    }
    //获取ACCESSTOKEN
    public function sAcessToken(){
      $this->HandleAT = $this->gAccessToken();
      if($this->HandleAT->expire_time < time()){
        $appid = $this->AppID;
        $appsecret = $this->AppSecret;
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        $result = https_request($url);
        //echo '<pre>'; var_dump($result);die;
        $jsoninfo = json_decode($result, true);
        $access_token = $jsoninfo["access_token"];
        $this->pAccessToken($access_token);
        return $access_token;
      }
      else{
        return $this->HandleAT->access_token;
      }
    }
    //保存ACCESSTOKEN
    public function pAccessToken($accesstoken){
      $Path = $_SERVER['DOCUMENT_ROOT']."/jSon_file/access_token_".$this->ID.".json";
      //print_r($Path);
      if(!file_exists($Path)){
        touch($Path);
        chmod($Path,0777);
      }
      $data['expire_time'] = time()   7000;
      $data['access_token'] = $accesstoken;
      $fp = fopen($Path, "w");
      fwrite($fp, json_encode($data));
      fclose($fp);
    }
    //读取ACCESSTOKEN
    public function gAccessToken(){
      $Path = $_SERVER['DOCUMENT_ROOT']."/jSon_file/access_token_".$this->ID.".json";
      if(!file_exists($Path)){
        $data['expire_time'] = 0;
        $data['access_token'] = '';
      }
      else{
        $data = json_decode(file_get_contents($Path));
        //print_r($data);
      }
      return $data;
    }
    //获取所有商品
    public function gStateProduct($state = 0){
       //https://api.weixin.qq.com/merchant/getbystatus?access_token=ACCESS_TOKEN
       //{"status": 0}
       $this->AccessToken = $this->sAcessToken();
       $url = "https://api.weixin.qq.com/merchant/getbystatus?access_token=".$this->AccessToken;
       //print_r($this->AccessToken);
       $ResData = $this->cUrlRequest($url,'{"status": '.$state.'}');
       //echo "<pre>";
       print_r( json_decode($ResData) );
    }
    //设置微小店物流支持列表
    public function sLogisticsList(){
      $this->Logistics['Fsearch_code'] = "邮政EMS";
      $this->Logistics['002shentong'] = "申通快递";
      $this->Logistics['066zhongtong'] = "中通速递";
      $this->Logistics['056yuantong'] = "圆通速递";
      $this->Logistics['042tiantian'] = "天天快递";
      $this->Logistics['003shunfeng'] = "顺丰速运";
      $this->Logistics['059Yunda'] = "韵达快运";
      $this->Logistics['064zhaijisong'] = "宅急送";
      $this->Logistics['020huitong'] = "汇通快运";
      $this->Logistics['zj001yixun'] = "易迅快递";
    }
    //获取订单详情
    public function gOrderInfo($order){
      $this->AccessToken = $this->sAcessToken();
      //print_r($this->AccessToken);
      $url = "https://api.weixin.qq.com/merchant/order/getbyid?access_token=".$this->AccessToken;
      $ResData = $this->cUrlRequest($url,'{"order_id": "'.$order.'"}');
      //$url = "https://api.weixin.qq.com/merchant/order/getbyfilter?access_token=".$this->AccessToken;
      //$ResData = $this->cUrlRequest($url,'{"status": 2}');
      print_r( json_decode($ResData) );
    }
    //查询全部订单
    public function gOrderAll($data = array()){
      $this->AccessToken = $this->sAcessToken();
      $url = "https://api.weixin.qq.com/merchant/order/getbyfilter?access_token=".$this->AccessToken;
      if(!empty($data)){
        $data = json_encode($data);
      }
      else{
        $firstday = strtotime(date("Y-m-01",time()));
        $data = array('begintime' => $firstday,'endtime' => strtotime("$firstday  1 month -1 day"));
        $data = json_encode($data);
      }
      $ResData = $this->cUrlRequest($url,$data);
      print_r( json_decode($ResData) );
    }
    //设置订单发货
    public function sOrderDelivery($data = array("need_delivery" => '0')){
      $this->AccessToken = $this->sAcessToken();
      $url = "https://api.weixin.qq.com/merchant/order/setdelivery?access_token=".$this->AccessToken;
      if(!empty($data)){
        $data = json_encode($data);
      }
      else{
        $data = array("need_delivery" => '0');
        $data = json_encode($data);
      }
      $ResData = $this->cUrlRequest($url,$data);
      print_r( json_decode($ResData) );
    }
    //关闭订单
    public function sOrderClose($order){
      $this->AccessToken = $this->sAcessToken();
      $url = "https://api.weixin.qq.com/merchant/order/close?access_token=".$this->AccessToken;
      $ResData = $this->cUrlRequest($url,'{"order_id": "'.$order.'"}');
      print_r( json_decode($ResData) );
    }
}

您可能感兴趣的文章:

  • PHP的switch判断语句的“高级”用法详解

上面的字体可以随意设置。

  1. 页面执行代码

下面的代码讲返回一个图像,实例化Captcha类后动态生成的一个图像。(outCaptcha.php)

<?php
include_once 'class/weixiaodian.php';
$wXd = new wXd();
echo "<pre>";
//查询全部商品
$wXd->gStateProduct();
//获取订单信息
$wXd->gOrderInfo('12963133879983601645');
//关闭订单
$wXd->sOrderClose('12963133879983600740');
//发货订单设置
$data['need_delivery'] = '1';
$data['order_id'] = '12963133879983600667';
$data['delivery_company'] = '059Yunda';
$data['delivery_track_no'] = '1000464090326';
$wXd->sOrderDelivery($data);
//获取所有订单
$wXd->gOrderAll();
echo "</pre>";
<?php

require('Captcha.php');
$code= new Captcha();
header('Content-Type:image/png');
$code->execute();

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

header(‘Content-Type:image/png');

希望本文所述对大家PHP程序设计有所帮助。

本文由网上十大正规赌博平台发布于正规赌博平台,转载请注明出处:用session与gd库实现简单验证码生成与验证的类方

关键词: EBET视讯直播