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);
session
- session是一种保存上下文信息的机制,保存的是对象,它的值是存放在服务器端,它通过sessionld 来区分不同的客户端,而sessionld是保存在客户端的, 做为客户端与服务器的验证标识,它是一个 24位的随机字符串,用户每次提交页面时,浏览器都会把这个sessionld包含在HTTP头中提交给WEB服务器。
session应用
- 生成session并且产生验证码图片
- web端输入并提交数据
- 服务端依据session判断提交的验证码输入是否正确
<?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>
<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>
<?php
Class Code{
public $width;
public $height;
public $numbers;
public $codeType;
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:
$this->codeString = join('',array_rand(range(0, 9),$this->numbers));
break;
case 2:
$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;
}
private function show(){
$this->createImg();
$this->fillBackground();
$this->fillPix();
$this->fillArc();
$this->writeFont();
$this->outPutImg();
}
private function createImg(){
$this->resource = imagecreatetruecolor($this->width,$this->height);
}
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());
}
private function fillPix()
{
$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());
}
}
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());
}
}
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/'.$this->imageType);
$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;
运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4fe1cef5e39e17a4b41b0996570c3398.png)