SESSION案例用法

cookie

  • Cookie是-种能够让网站服务 器能把少嚴数据储存到客户端的硬 盘或内存,或是从客户端的硬盘读取数据的一种技术
  • cookie是由服务器生成的,服务器就是我们的这个php,它会往我们这个浏览器发送一段ASCLL文本,当浏览器收到后,会将其这个片段以键值对的形式保存在某个目录下,其实也是保存在我们的浏览器上

setcookie(name,value,expire,path,domain,secure,httponly)

  • name:必填,规定cookie的名称
  • value:必填,规定cookie的值
  • expire:选填,规定cookie的有效期
  • path:选填,规定cookie服务器路径
  • domain:选填,规定cookie的域名
  • secure:选填,规定是否通过安全的https连接来传递cookie
  • httponly:选填,只允许http请求的访问获取cookie
setcookie("code",$this->codeString,time()+600);//设置过期时间为10分钟

session

  • session是一种保存上下文信息的机制,保存的是对象,它的值是存放在服务器端,它通过sessionld 来区分不同的客户端,而sessionld是保存在客户端的, 做为客户端与服务器的验证标识,它是一个 24位的随机字符串,用户每次提交页面时,浏览器都会把这个sessionld包含在HTTP头中提交给WEB服务器。
session应用
  1. 生成session并且产生验证码图片
  2. web端输入并提交数据
  3. 服务端依据session判断提交的验证码输入是否正确
//client.php

<?php
session_start();
var_dump($_SESSION['veri_code']);

$data = $_POST['code'];
var_dump($data);
if (isset($_SESSION['veri_code'])){
    echo "验证码session存在"."<br>";
    if ($_SESSION['veri_code'] == $data){
        unset($_SESSION['veri_code']);
        $result = "验证码输入正确"."<br>";
    }else{
        $result = "验证码输入错误"."<br>";
    }
}else{
    $result = "验证码session不存在"."<br>";
}
var_dump($result);
return json_encode($result);
<!DOCTYPE html>
<!--这里是表单页面,web.html-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="./client.php" method="post">
        <span >验证码:</span>
        <span><input type="text" name="code" class="code" id="code" placeholder="请输入验证码"></span>
        <span><img src="captcha.php" onClick="this.src='captcha.php?nocache='+Math.random()" style="cursor:pointer"></span>
        <br>
        <button >提交</button>
    </form>
</body>
</html>
//captcha.php

<?php

/*
{
属性:
宽度 int $width
高度 int $hight
干扰点
验证码(私有的)
验证码字符的个数 int $number
验证码的类型(纯数字类型、纯字母类型、混合类型)

背景颜色 string $color
字体颜色 string $fontColor

图片类型(jpg、png、bmp……) string $imageType
图片资源 string $resource

功能:
构造函数(用来初始化) function
生成验证码的函数 function createCode

创建画布    function createImage
填充背景色  function fillBackground
填充干扰点
填充干扰弧线
画验证码
输出图片
}


 */

Class Code{

    public $width;
    public $height;
    public $numbers;
    public $codeType;    //1.数字 2.字母 3.混合
    public $color;
    public $fontColor;
    public $imageType;

    private $codeString;//验证字符串母体
    private $resource;  //图片资源

    public function __construct($w=100,$h=50,$n=4,$imageType='png',$codeType=1){
        $this->width = $w;
        $this->height = $h;
        $this->numbers = $n;
        $this->imageType = $imageType;
        $this->codeType = $codeType;

        /*    生成随机的验证字符串    */
        $this->codeString = $this->createCode($this->codeType);
        session_start();
        $_SESSION['veri_code']=$this->codeString;
        $this->show();
    }

    private function createCode($type){
        switch($type){
            case 1:
                /*生成纯数字,首先使用range(0,9)生成数组
                  *通过$this->verifyNums确定字符串的个数
                  *使用array_rand()从数组中随机获取相应个数
                  *使用join将数字拼接成字符串,存储到$this->codeString中
                 */
                $this->codeString = join('',array_rand(range(0, 9),$this->numbers));
                break;
            case 2:
                /*生成纯字母,
                   *小写字母数组range('a','z')
                   *大写字母数组range('A','Z')
                   *合并两个数组array_merge()
                   *更换键和值  array_filp()
                   *随机获取数组中的特定个数的元素 array_rand()
                   *拼接成字符串 implode()
                */
                $this->codeString = implode(array_rand(array_filp(array_merge(range('a','z'),range('A','Z'))),$this->numbers));
            case 3:
                //混合类型
                $words = str_shuffle('abcdefghjkmpopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789');
                $this->codeString = substr($words,0,$this->numbers);
                break;
        }
        return $this->codeString;
    }


    //开始准备生成图片
    /*方法名:show()
     *功能  :调用生成图片的所有方法
     */
    private function show(){
        $this->createImg();//创建图片资源
        $this->fillBackground();   //填充背景颜色
        $this->fillPix();  //填充干扰点
        $this->fillArc();  //填充干扰弧线
        $this->writeFont();//写字
        $this->outPutImg();//输出图片
    }

    //创建图片资源:imagecreatetruecolor($width,$height)
    private function createImg(){
        $this->resource = imagecreatetruecolor($this->width,$this->height);
    }

    //填充背景颜色:imagefill($res,$x,$y,$color)
    //随机生成深色--->imagecolorallocate($res,$r,$g,$b)
    private function setDarkColor()
    {
        return imagecolorallocate($this->resource,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
    }
    //随机生成浅色
    private function setLightColor()
    {
        return imagecolorallocate($this->resource,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));
    }
    //开始填充
    private function fillBackground()
    {
        imagefill($this->resource,0,0,$this->setDarkColor());
    }

    //随机生成干扰点-->imagesetpixel
    private function fillPix()
    {
        //计算产生多少个干扰点(单一像素),这里设置每20个像素产生一个
        $num = ceil(($this->width * $this->height) / 20);
        for($i = 0; $i < $num; $i++){
            imagesetpixel($this->resource,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->setDarkColor());
        }
    }

    //随机画10条弧线->imagearc()
    private function fillArc()
    {
        for($i = 0;$i < 10;$i++){
            imagearc($this->resource,
                mt_rand(10,$this->width-10),
                mt_rand(5,$this->height-5),
                mt_rand(0,$this->width),
                mt_rand(0,$this->height),
                mt_rand(0,180),
                mt_rand(181,360),
                $this->setDarkColor());
        }
    }


    /*在画布上写文字
     *根据字符的个数,将画布横向分成相应的块
      $every = ceil($this->width/$this->verifyNums);
     *每一个小块的随机位置画上对应的字符
      imagechar();
     */

    private function writeFont()
    {
        $every = ceil($this->width / $this->numbers);
        for($i = 0;$i < $this->numbers;$i++){
            $x = mt_rand(($every * $i) + 5,$every * ($i + 1) - 5);
            $y = mt_rand(5,$this->height - 10);

            imagechar($this->resource,6,$x,$y,$this->codeString[$i],$this->setLightColor());
        }
    }

    //输出图片资源
    private function outPutImg()
    {
        //header('Content-type:image/图片类型')
        header('Content-type:image/'.$this->imageType);

        //根据图片类型,调用不同的方法输出图片
        //imagepng($img)/imagejpg($img)
        $func = 'image'.$this->imageType;
        $func($this->resource);
    }

    //设置验证码字符只能调用,不能修改,用来验证验证码是否输入正确
    public function __get($name){
        if($name = 'codeString'){
            return $this->codeString;
        }
    }

    //析构方法,自动销毁图片资源
    public function __destruct()
    {
        imagedestroy($this->resource);
    }

}
//这个new Code不能少,否则图片无法加载,不要再范
new Code;
运行截图

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python小叮当

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值