php一个类一个校验的简单介绍

发布时间:2022-11-10

本文目录一览:

  1. thinkphp 怎么做登录验证
  2. THINKPHP如何能让一个类中的所有方法在执行前都先执行一个验证方法
  3. 怎么在php中判断某个类方法是否存在且能被调用
  4. 求一个php验证码及如何使用的例子?
  5. php判断一个类里面的某个函数是否存在

thinkphp 怎么做登录验证

登录无非就是验证用户名密码以及验证码是否正确,我们可以新建一个CommonAction的公共类,用来校验权限,其他所有类继承此类。该类内部写一个初始化方法,用于验证,这一讲先不详细讲解。继续说登录,由于登录是公开模块的方法,所以可以新建一个PublicAction类,用于公共的免验证方法,同时在配置文件中添加

'NOT_AUTH_MODULE'='Public',//默认不需要认证的模块
'USER_AUTH_GATEWAY'='/Public/login',//默认的认证网关

然后开始编写Public类,具体代码如下:

<?php
class PublicAction extends CommonAction {
    //验证码显示
    public function verify() {
        import("ORG.Util.Image");
        Image::buildImageVerify(4,1,"png",100,28,"verify");
    }
    //验证是否账号密码
    function checklogin() {
        //此处多余可自行改为Model自动验证
        if(empty($_POST['username'])) {
            $this->error('帐号错误!');
        } elseif (empty($_POST['password'])) {
            $this->error('密码必须!');
        } elseif (empty($_POST['verify'])) {
            $this->error('验证码必须!');
        }
        $map = array();
        $map['username'] = $_POST['username'];
        $map['status'] = array('gt',0);
        if($_SESSION['verify'] != md5($_POST['verify'])) {
            $this->error('验证码错误!');
        }
        import('ORG.Util.RBAC');
        //C('USER_AUTH_MODEL','User');
        //验证账号密码
        $authInfo = RBAC::authenticate($map);
        if(empty($authInfo)) {
            $this->error('账号不存在或者被禁用!');
        } else {
            if($authInfo['password'] != md5($_POST['password'])) {
                $this->error('账号密码错误!');
            } else {
                $_SESSION[C('USER_AUTH_KEY')] = $authInfo['id'];//记录认证标记,必须有。其他信息根据情况取用。
                $_SESSION['email'] = $authInfo['email'];
                $_SESSION['nickname'] = $authInfo['nickname'];
                $_SESSION['user'] = $authInfo['username'];
                $_SESSION['last_login_date'] = $authInfo['last_login_date'];
                $_SESSION['last_login_ip'] = $authInfo['last_login_ip'];
                //判断是否为超级管理员
                if($authInfo['username'] == 'admin') {
                    $_SESSION[C('ADMIN_AUTH_KEY')] = true;
                }
                //以下操作为记录本次登录信息
                $user = M('User');
                $lastdate = date('Y-m-d H:i:s');
                $data = array();
                $data['id'] = $authInfo['id'];
                $data['last_login_date'] = $lastdate;
                $data['last_login_ip'] = $_SERVER["REMOTE_ADDR"];
                $user->save($data);
                RBAC::saveAccessList();//用于检测用户权限的方法,并保存到Session中
                $this->assign('jumpUrl', './Index/index');
                $this->success('登录成功!');
            }
        }
    }
    //退出登录操作
    function logout() {
        if(!empty($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            $_SESSION = array();
            session_destroy();
            $this->assign('jumpUrl', '/Code'.'/login');
            $this->success('登出成功');
        } else {
            $this->error('已经登出了');
        }
    }
}
?>

以上代码仅实现功能,没有做优化,有些验证的操作可以放到model,session也不用一 一赋值,用数组即可,我想已经入门的应该可以自己改的更好。

THINKPHP如何能让一个类中的所有方法在执行前都先执行一个验证方法

首先在你的方法类中写一个public function _initialize这个方法 在这个方法里面调用你的验证方法 这样在这个类里面所有方法执行前都会先执行一遍这个方法 如果在别的类里面的方法执行前也要执行这个验证方法 那就别的类继承这个类 然后就可以了

怎么在php中判断某个类方法是否存在且能被调用

先用 include或require将文件包含到你需要调用该类的文件中

<?php
include("class.php");//将目标文件包含进来
$className = new Class();//将目标类实例化
$className->show(); //这样访问目标类里面的方法。
?>

要是楼楼还不懂,可以上后盾网问问昂。教学视频不仅多,都是一线讲师亲自录制,含金量高

求一个php验证码及如何使用的例子?

以前项目中的东西 在没有GB库的情况下也可以生成验证码 关键部分做出了注释

<img src="image.php">
<?php
//存为文件 image.php
error_reporting(E_ALL ^ E_NOTICE);
require_once('image.class.php');
$image_model = new Image();
//可以在此处更改属性达到验证码不同大小的图片
$image_model->outputBrowser();
?>
<?php
/*
* 验证码生成类 存为文件image.class.php
* 如果不支持GD库,则采用像素画法
* 详见
*/
class Image {
    var $img_height = 20; //图片的长
    var $img_width = 68; //图片的宽
    var $img_cache = ''; //图像信息缓存
    var $check_number = ''; //验证码
    var $lenght = 4; //验证码位数
    var $is_gd = false;
    //'----------以下为不支持GD需要的变量
    var $pixels = '';
    var $digits = '';
    var $lines = '';
    var $data = '';
    var $chunk = '';
    var $crc_table = '';
    function Image() {
        session_start();
        session_register("check_number");
        $this->session = $_SESSION;
        $this->is_gd = function_exists('gd_info') ? true : false;
        $this->getCheckNumber();
        if( $this->is_gd ):
            $this->getFromGD();
        else:
            $this->getFromCode();
        endif;
    }
    //'----浏览器输出
    function outputBrowser() {
        if( $this->is_gd ) {
            Header("Content-type: image/png"); //告诉浏览器,下面的数据是图片,而不要按文字显示
            Imagepng($this->img_cache); //生成png格式。。。嘿嘿效果蛮像回事的嘛。。。
            ImageDestroy($this->img_cache);
        } else echo($this->img_cache);
    }
    //'----文件输出
    function outputFile($filename=null) {
        if(!$filename) $filename = time().'.png';
        $fp = fopen($filename, 'w+');
        fwrite($fp, $this->img_cache);
        fclose($fp);
    }
    //'----得到验证码
    function getCheckNumber() {
        //srand(microtime() * 100000);//PHP420后,srand不是必须的
        for($Tmpa=0;$Tmpa < $this->lenght;$Tmpa++) {
            $this->check_number .= $this->is_gd ? dechex(rand(0,15)) : rand(0,3);
        }
        unset($Tmpa);
        $this->session['check_number'] = strtoupper($this->check_number);
    }
    //'----用GD库生成
    function getFromGD() {
        //$black = '';
        $this->img_cache = imageCreate($this->img_width,$this->img_height); //生成图片
        //图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了
        $black = ImageColorAllocate($this->img_cache, 255,255,255);
        //下面该生成雪花背景了,其实就是在图片上生成一些符号
        //先用100个做测试
        /*
        for ($i=1; $i <= 15; $i++) {
            imageString($this->img_cache,1,mt_rand(1,$this->img_height-10),mt_rand(1,$this->img_width-10),".",
            imageColorAllocate($this->img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
            //哈,看到了吧,其实也不是雪花,就是生成*号而已。为了使它们看起来"杂乱无章、5颜6色",就得在1个1个生成它们的时候,让它们的位置、颜色,甚至大小都用随机数,rand()或mt_rand都可以完成。
        }
        */
        //上面生成了背景,现在就该把已经生成的随机数放上来了。道理和上面差不多,随机数1个1个地放,同时让他们的位置、大小、颜色都用成随机数~~
        //为了区别于背景,这里的颜色不超过200,上面的不小于200
        imagettftext($this->img_cache, 20, 0, 10, 20, $black, "./html/VINERITC.TTF", "AC67");
        for ($i=0;$i < strlen($this->session['check_number']);$i++) {
            imagettftext($this->img_cache, 14, rand(0,30), $i*$this->img_width/4 + 3, 17, imageColorAllocate($this->img_cache,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)), "./html/VINERITC.TTF", $this->session['check_number'][$i]);
        }
        for($i=0;$i < 100;$i++)//加入干扰象素
        {
            imagesetpixel($this->img_cache, rand()%70 , rand()%30 , imageColorAllocate($this->img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
        }
        $black = ImageColorAllocate($this->img_cache, 0,0,0); //定义需要的黑色
        //先成一黑色的矩形把图片包围
        ImageRectangle($this->img_cache,0,0, $this->img_width-1,$this->img_height-1, $black);
    }
    //End Function getGD
    //-----------------------------------------------------------------------------------------------------------------------
    function set_4pixel($r, $g, $b, $x, $y) {
        $ofs = 3 * ($this->img_width * $y + $x);
        $this->pixels[$ofs] = chr($r);
        $this->pixels[$ofs + 1] = chr($g);
        $this->pixels[$ofs + 2] = chr($b);
        $this->pixels[$ofs + 3] = chr($r);
        $this->pixels[$ofs + 4] = chr($g);
        $this->pixels[$ofs + 5] = chr($b);
        $ofs += 3 * $this->img_width;
        $this->pixels[$ofs] = chr($r);
        $this->pixels[$ofs + 1] = chr($g);
        $this->pixels[$ofs + 2] = chr($b);
        $this->pixels[$ofs + 3] = chr($r);
        $this->pixels[$ofs + 4] = chr($g);
        $this->pixels[$ofs + 5] = chr($b);
    }
    //生成数字图象的函数
    function draw2digits($x, $y, $number) {
        //$this->draw_digit($x, $y, intval($number / 10) );
        $this->draw_digit($x + 11, $y, $number % 10);
    }
    function draw_digit($x, $y, $digit) {
        $digit = $this->digits[$digit];
        $m = 8;
        for($b = 1, $i = 0; $i < 7; $i++, $b *= 2) {
            if(($b & $digit) == $b) {
                $j = $i * 4;
                $x0 = $this->lines[$j] * $m + $x;
                $y0 = $this->lines[$j + 1] * $m + $y;
                $x1 = $this->lines[$j + 2] * $m + $x;
                $y1 = $this->lines[$j + 3] * $m + $y;
                if($x0 == $x1) {
                    $ofs = 3 * ($this->img_width * $y0 + $x0);
                    for($h = $y0; $h <= $y1; $h++, $ofs += 3 * $this->img_width) {
                        $this->pixels[$ofs] = chr(0);
                        $this->pixels[$ofs + 1] = chr(0);
                        $this->pixels[$ofs + 2] = chr(0);
                    }
                } else {
                    $ofs = 3 * ($this->img_width * $y0 + $x0);
                    for($w = $x0; $w <= $x1; $w++) {
                        $this->pixels[$ofs++] = chr(0);
                        $this->pixels[$ofs++] = chr(0);
                        $this->pixels[$ofs++] = chr(0);
                    }
                }
            }
        }
    }
    //将文字加入到图象中
    function add_chunk($type) {
        // chunk :为层
        // length: 4 字节: 用来计算 chunk
        // chunk type: 4 字节
        // chunk data: length bytes
        // CRC: 4 字节: 循环冗余码校验
        // copy data and create CRC checksum
        $len = strlen($this->data);
        $this->chunk = pack("c*", ($len >> 24) & 255,
            ($len >> 16) & 255,
            ($len >> 8) & 255,
            $len & 255);
        $this->chunk .= $type;
        $this->chunk .= $this->data;
        //calculate a CRC checksum with the bytes chunk[4..len-1]
        $z = 16777215;
        $z |= 255 << 24;
        $c = $z;
        for ($n = 4; $n < strlen($this->chunk); $n++) {
            $c8 = ($c >> 8) & 0xffffff;
            $c = $this->crc_table[($c ^ ord($this->chunk[$n])) & 0xff] ^ $c8;
        }
        $crc = $c ^ $z;
        $this->chunk .= chr(($crc >> 24) & 255);
        $this->chunk .= chr(($crc >> 16) & 255);
        $this->chunk .= chr(($crc >> 8) & 255);
        $this->chunk .= chr($crc & 255);
        //将结果加到$img_cache中
        $this->img_cache .= $this->chunk;
    }
    //主程序
    function getFromCode() {
        //填充
        for($h = 0; $h < $this->img_height; $h++) {
            for($w = 0; $w < $this->img_width; $w++) {
                $r = 100 / $this->img_width * $w + 155;
                $g = 100 / $this->img_height * $h + 155;
                $b = 255 - (100 / ($this->img_width + $this->img_height) * ($w + $h));
                $this->pixels .= chr($r);
                $this->pixels .= chr($g);
                $this->pixels .= chr($b);
            }
        }
        $this->digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
        $this->lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);
        for($i=0; $i < $this->lenght; $i++) {
            $this->draw2digits($i*13, 2, $this->check_number[$i]);
        }
        //$this->set_4pixel(0, 0, 0, 26, 7);
        //$this->set_4pixel(0, 0, 0, 26, 13);
        //$this->set_4pixel(0, 0, 0, 52, 7);
        //$this->set_4pixel(0, 0, 0, 52, 13);
        //创建循环冗余码校验表
        $z = -306674912;// = 0xedb88320
        for ($n = 0; $n < 256; $n++) {
            $c = $n;
            for ($k = 0; $k < 8; $k++) {
                $c2 = ($c >> 1) & 0x7fffffff;
                if($c & 1) $c = $z ^ ($c2);
                else $c = $c2;
            }
            $this->crc_table[$n] = $c;
        }
        // PNG file signature
        $this->img_cache = pack("c*", 137,80,78,71,13,10,26,10);
        // IHDR chunk data:
        // width: 4 bytes
        // height: 4 bytes
        // bit depth: 1 byte (8 bits per RGB value)
        // color type: 1 byte (2 = RGB)
        // compression method: 1 byte (0 = deflate/inflate)
        // filter method: 1 byte (0 = adaptive filtering)
        // interlace method: 1 byte (0 = no interlace)
        $this->data = pack("c*", ($this->img_width >> 24) & 255,
            ($this->img_width >> 16) & 255,
            ($this->img_width >> 8) & 255,
            $this->img_width & 255,
            ($this->img_height >> 24) & 255,
            ($this->img_height >> 16) & 255,
            ($this->img_height >> 8) & 255,
            $this->img_height & 255, 8, 2, 0, 0, 0);
        $this->add_chunk("IHDR");
        //以下不敢乱翻译,请自行参考
        // scanline:
        // filter byte: 0 = none
        // RGB bytes for the line
        // the scanline is compressed with "zlib", method 8 (RFC-1950):
        // compression method/flags code: 1 byte ($78 = method 8, 32k window)
        // additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)
        // compressed data blocks: n bytes
        // one block (RFC-1951):
        // bit 0: BFINAL: 1 for the last block
        // bit 1 and 2: BTYPE: 0 for no compression
        // next 2 bytes: LEN (LSB first)
        // next 2 bytes: one's complement of LEN
        // LEN bytes uncompressed data
        // check value: 4 bytes (Adler-32 checksum of the uncompressed data)
        $len = ($this->img_width * 3 + 1) * $this->img_height;
        $this->data = pack("c*", 0x78, 0x01, 1, $len & 255, ($len >> 8) & 255, 255 - ($len & 255), 255 - (($len >> 8) & 255) );
        $start = strlen($this->data);
        $i2 = 0;
        for($h = 0; $h < $this->img_height; $h++) {
            $this->data .= chr(0);
            for($w = 0; $w < $this->img_width * 3; $w++) {
                $this->data .= $this->pixels[$i2++];
            }
        }
        //calculate a Adler32 checksum with the bytes data[start..len-1]
        $s1 = 1;
        $s2 = 0;
        for ($n = $start; $n < strlen($this->data); $n++) {
            $s1 = ($s1 + ord($this->data[$n])) % 65521;
            $s2 = ($s2 + $s1) % 65521;
        }
        $adler = ($s2 << 16) | $s1;
        $this->data .= chr(($adler >> 24) & 255);
        $this->data .= chr(($adler >> 16) & 255);
        $this->data .= chr(($adler >> 8) & 255);
        $this->data .= chr($adler & 255);
        $this->add_chunk("IDAT");
        //IEND: marks the end of the PNG-file
        $this->data = "";
        $this->add_chunk("IEND");
        //列印图象
        //echo($this->img_cache);
    }
}
?>

php判断一个类里面的某个函数是否存在

  1. 首先需要新建一个246.php。
  2. 然后需要按照图示代码输入php网页的结构(<?php?>)。
  3. 然后需要按照图示代码声明PHP与浏览器交互的文件类型和编码。
  4. function_exists() 函数的作用: 如果函数已被定义就返回 TRUE,如图所示为其语法结构。
  5. 然后需要按照图示代码使用 function_exists() 函数判断 show() 函数是否已经被定义。
  6. 运行该网页,输出 function_exists() 函数的判断结果,如图显示函数不存在。