demo 文件执行错误
在视频5:50左右,执行demo.php的时候产生以下错误信息(下载课程提供的源码来执行也是一样):

demo.php代码
<?php session_start(); var_dump($_SESSION['captcha_code']);
Captcha.php文件代码:
<?php
/**
* Captcha.php
* author: F.X
* date: 2017
* description 验证码类
*/
namespace Imooc\Lib;
require_once 'GDBasic.php';
class Captcha extends GDBasic
{
//图像宽度
protected $_width = 60;
//图像高度
protected $_height = 25;
//随机串
protected $_code = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjklmnpqrstuvwxyz';
//字体文件
protected $_font_file = './font/comic.ttf';
//图像
protected $_im;
//验证码
protected $_captcha;
public function __construct($width = null, $height = null)
{
self::check();
$this->create($width, $height);
}
/**
* 创建图像
* @param $width
* @param $height
*/
public function create($width, $height)
{
$this->_width = is_numeric($width) ? $width : $this->_width;
$this->_height = is_numeric($height) ? $height : $this->_height;
//创建图像
$im = imagecreatetruecolor($this->_width, $this->_height);
$back = imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
//填充底色
imagefill($im, 0, 0, $back);
$this->_im = $im;
}
/**
* 混乱验证码
*/
public function moll()
{
$back = imagecolorallocate($this->_im, 0, 0, 0);
//在图像中随机生成50个点
for($i = 0; $i < 50; $i++)
{
imagesetpixel($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
}
imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
}
/**
* 生成验证码随机串
* @param int $length 验证码的个数
* @param int $fontSize 字符串的字体大小
* @return Captcha
*/
public function string($length = 4, $fontSize = 15)
{
$this->moll();
$code = $this->_code;
$captcha = '';
for($i = 0; $i < $length; $i++)
{
$string = $code[mt_rand(0, strlen($code) - 1)];
$strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150));
imagettftext($this->_im, $fontSize, mt_rand(-10, 10), mt_rand(3, 6) + $i * (($this->_width - 10) / $length), ($this->_height / 3) * 2, $strColor, $this->_font_file, $string);
$captcha .= $string;
}
$this->_captcha = $captcha;
return $this;
}
/**
* 验证码存入session
*/
public function setSession()
{
if(!isset($_SESSION))
{
session_start();
}
$_SESSION['captcha_code'] = $this->_captcha;
}
/**
* 逻辑运算符验证码
* @param int $fontSize 字体大小
* @return $this
*/
public function logic($fontSize = 12)
{
$this->moll();
$codeArray = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9);
$operatorArray = array('+' => '+', '-' => '-', 'x' => '*');
list($first, $second) = array_rand($codeArray, 2);
$operator = array_rand($operatorArray);
$captcha = 0;
$string = '';
switch($operator)
{
case '+':
$captcha = $first + $second;
break;
case '-':
//当第一个数小于第二个数
if($first < $second)
{
list($first, $second) = array($second, $first);
}
$captcha = $first - $second;
break;
case 'x':
$captcha = $first * $second;
break;
}
//设置验证码类变量
$this->_captcha = $captcha;
//要输出到图像中的字符串
$string = sprintf('%s%s%s=?', $first, $operator, $second);
$strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150));
imagettftext($this->_im, $fontSize, 0, 5, ($this->_height / 3) * 2, $strColor, $this->_font_file, $string);
return $this;
}
/**
* 输出验证码
*/
public function show()
{
//生成session
$this->setSession();
header('Content-Type:image/jpeg');
imagejpeg($this->_im);
imagedestroy($this->_im);
}
}18
收起
正在回答
1回答
你好,这里报的是notice错误,意思是session中没有键名为captcha_code这个元素,需要先执行index.php,用Captcha.php中类的show方法将数据写进session,然后再执行demo.php从session中读取,如下:

这是index.php的代码,下载的源码是老师课堂最后的代码,那时候这部分代码已经注释了,同学如果用源码测试的话,需要根据视频内容,将这部分代码启用,其他代码注释才能正确运行哦~
如果解决了您的问题,请采纳,祝学习愉快~
PHP常用技术与ThinkPHP5框架开发
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星