PHP工具類,開發系統必備的類別: 表單驗證類別 驗證碼類 日誌類別 分頁類別 無限極分類類
-
class Lib_Form
- {
- private $typeArr=array('isNotEmpty' , 'isInt' , 'isStr' , 'isEmail' , ' isTel' , 'isOnlyNum' , 'hasSet', 'isOnlyChar' , 'isNumAndChar' , 'checkLength');
- private $msg = array();
- private $code = 0;
- {
- if(!is_array($post))
- {
- $this->msg[] = '資料不是備份';
- }
- else
- {
- foreach ($post as $field=>$value)
- {
- $func = $post[$field]['valid'];
- $value = $ post[$field]['value'];
-
- $checkLength = 'checkLength';
- if($pos = stripos($func , $checkLength)!=false)
- {
- $condition = substr($func, strlen($checkLength));
- $func = $checkLength;
- $lengthArr =explode('-', $condition);
- self :: $func($value , $field , $lengthArr[0] , $lengthArr[1]);
- }
- else
- {
- if(!in_array($func , $this- >typeArr) ))
- {
- $this->msg = $func。 isNotExists';
- 規則;
- }
- self::$func($value , $field);
- }
- }
- }
- return $this->showRestult ( );
- }
-
- 牛皮函數showRestult()
- {
- if($this->msg && is_array($this->msg))
- {
- $this ->code = 1;
- $msg = implode(',', $this->msg);
- $ret = array('code'=>$this->code , 'msg'=>$ msg);
- return $ret;
- }
- return array('code'=>$this->code , 'msg'=>'成功' );
- }
-
- private function isNotEmpty($value,$field)
- {
- if(!$this->hasSet($value, $field)) return false;
- $value = trim($value);
- if(empty($value))
- {
- $this->msg[] = $field.' isEmpty';
- return false;
- }
- return true;
- }
-
- private function isInt($value,$field)
- {
- if(! this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- if(!is_int($value))
- {
- $this- > msg[] = $字段。 isNotInt';
- return false;
- }
- return true;
- }
-
- private function isStr($value,$field)
- {
- if(!$ this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- if(!is_string($value))
- {
- $this- > msg[] = $字段。 isNotStr';
- return false;
- }
- return true;
- }
-
- private function hasSet($value , $field)
- {
- {
- if(!issfield)
- {
- if(! ($value))
- {
- $this->msg[] = $field.' isNotSet';
- return false;
- }
- return true;
- }
-
- private function isEmail($value,$field)
- {
- {
- if! this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[a-zA-Z0-9_-] @[a - zA-Z0-9_-] (.[a-zA-Z0-9_-] )$/";
- if(!preg_match($pattern, $value))
- {
- $this- > msg[] = $字段。 isNotEmail';
- return false;
- }
- return true;
- }
-
- private function isTel($value,$field)
- {
- if(! this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = '/^[0-9]{7,11}$/' ;
- if (!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotTel';
- return false;
- }
- return true;
- }
-
- private function isOnlyNum($value,$field)
- {
- {
- if( this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[0-9]{1,}$/";
- if(!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotOnlyNum';
- return false;
- }
- return true;
- }
-
- private function isOnlyChar($value,$field)
- {isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[a-zA-Z]{1,}$/ " ;
- if(!preg_match($pattern, $value))
- {
- $this->msg[] = $field.' isNotOnlyChar';
- return false;
- }
- return true;
- }
-
- private function isNumAndChar($value,$field)
- {isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $pattern = "/^[a-zA-z0-9]{1,} $ /";
- if(!preg_match($pattern , $value))
- {
- $this->msg[] = $field.' isNotNumAndChar';
- 回傳 false;
- } 回傳 true; }
-
- 導管函數 checkLength($value , $field , $minLength , $maxLength)
- {
- if(!$this->isNotEmpty($value,$field)) return false;
- $value = trim($value);
- $length = (strlen($value) mb_strlen($value,'UTF8')) / 2;
- if($length $maxLength)
- {
- $this->msg[] = $field.' isNotInLength';
- 回傳 false;
- }
- 回傳 true;
- }
- }
-
- if($_POST['提交'])
- {
- if($_POST['提交'])
- {
- {
- $form = new Lib_Form();
- $post['name'] = array('value'=>$_POST['name'] , 'valid'=>'checkLength6-12');
- $ post['pwd'] = array('value'=>$_POST['pwd'] , 'valid'=>'checkLength4-12');
- $post['sex'] = array('value' =>$_POST['sex'] , 'valid'=>'hasSet');
-
- $ret = $form->validata($post);
- if ($ret['code' ])
- {
- echo $ret['msg'];
- }
- }
?>
複製代碼
-
-
-
-
class Lib_Image
- {
- private $height = 0;
- private $width = 0;
-
- {
- $this->height = $height;
- $this->width = $width;
- }
-
- 私有函數genCode( $num)
- {
- for($i=0;$i {
- switch(rand(0,2))
- {
- case 0:$code[$i]=chr(rand(48,57));break;//數字
- case 1:$code[$i]=chr(rand(65) ,90 ));break;//大寫字母
- case 2:$code[$i]=chr(rand(97,122));break;//小寫字母
- }
- }
- $ _SESSION[ "驗證碼"]=$驗證碼;
- 回傳 $code;
- }
-
- private function genOther($image)
- {
- for($i=0;$i {
- $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
- imagesetpixel($image,rand(1,$this->寬度),rand(1 , $this->height),$dis_color);
- }
- }
-
- public function veryCode()
- {
-
- $image=imagecreate($this- >寬度,$this->高度);
- imagecolorallocate($image,255,255,255);
- //$this->genOther($image);
-
- $num = 4;
- $code = $this->genCode($num);
- for($i=0;$i {
- $ char_color =imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
- imagechar($image,60,($this->width/$num)*$i, rand( 0,5),$code[$i],$char_color);
- }
-
- header("內容類型:image/png");
- imagepng($image); //圖片輸出到瀏覽器
- imagedestroy($image);//釋放資源
- }
- }
-
$image = new Lib_Image(25, 65); $image = new Lib_Image(25, 65); $image ->veryCode();?>
複製程式碼
-
-
-
-
-
class Lib_Log
- {
- private $logError = 0;
- {
- private $logError = 0;
- private $privwarn = 1; 原生$logDir = 'log/';
- 原生$logFile = 'log';
- 原生$fileExt = '.txt';
- 原生$fileHander = null;
-
- public function __construct()
- {
- if(!is_dir($this->logDir)){
- mkdir($this->logDir,0777);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- $this->logFile .= date('Y-m-d').$this->fileExt;
- if(!$this->fileHander = @fopen($this->logDir) .$this-> logFile, 'a ')){
- die('日誌檔打不開!');
} } public function writeLog($message) { $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; $debug = debug_backtrace(true); $string = date('Y-m-d H:i:s')."t"; $string .= $ip."t"; $string .=$debug[0]['file']."t"; $string .= "tline" . $debug[0]['line']."t"; $string .= json_encode($message)."rn"; if(!fwrite($this->fileHander, $string) ) { die('日誌檔無法寫入!'); } } public function __destruct() { if($this- >fileHander! =null){ fclose($this->fileHander); } } } $log = new Lib_Log(); $log->writeLog('錯誤調試!'); echo ""; ?>複製程式碼
-
class Lib_Page
- {
- public $currentPage=0; //目前頁數
- private $totalPage=0;頁數
- private $totalNums=0; //總記錄數
- private $perNums=0; //每頁顯示的記錄數
- private $type = 0; //顯示類型
-
- public function __construct($totalNums , $perNums,$type=0)
- {
- $this->totalNums = intval($totalNums);
- $this->perNums = intval($totalNums);
- $this->perNums = intval($perNums = intval($perNums = mper) ;
- $this->totalPage = intval(ceil($this->totalNums / $this->perNums));
- $this->currentPage = min(max(1 , $_REQUEST['p'] ) , $this->totalPage);
- $this->type = intval($type);
- }
-
- private function first()
- {
- if ($this ->currentPage==1) return false;
- return "首頁 ";
- }
-
- private function last()
- {
- if ($this->currentPage==$this->totalPage) return false;
- return "尾頁 ";
- }
-
- private function next()
- {
- $p = min($this->currentPage 1 , $this->totalPage);
- if ($ p==$this->totalPage) return false;
- return "下一頁 ";
- }
-
- private function prev()
- {
- $p = max(1 , $this->currentPage - 1);
- if($p==1) return false;
- return "上一頁 ";
- }
-
- private function total()
- {
- return "共{$ this->totalPage} 頁 | {$this->totalNums} 筆記錄 | 目前第{$this->currentPage} 頁";
- }
-
- private function page()
- {
- $show = "";
- for ($i=1; $itotalPage; $i ){
- if ($i==$this->currentPage)
- $show .= "{$i} " ;
- else
- $show .= "{$i} ";
- }
- return $show;
- }
-
- public function show()
- {
- if ($this->type==1) {
- return $this->total().' '.$this- >page();
- }else if($this->type==2){
- return $this->total().' '.$this->first().' '.$this ->prev().' '.$this->next().' '.$this->last();
- }elseif ($this->type==0){
- return $this ->total().' '.$this->first().' '.$this->prev().' '.$this->page().' '.$this->next(). ' '.$this->last();
- }
- }
-
- }
-
- $totalNums = 80;
- $perNums = 10;
- $page = new Lib_Page($totalNums, $perNums);
- echo $page->show();
?>
複製程式碼
-
class Lib_Tree
- {
- private $items = array();
- private $icon = arravate $items = array();
- private $icon = array( '✔ ,
- ' ├',
- ' ├',
- ' ├',
- ' 🎜> private $field = array( 'id','name');
- public $ret = '
類別名稱 | ; 操作 |
';-
- public function __construct($items)
- {
- $this->items = $items;
- }
-
- public function setIcon($icon)
- {
- $this->icon = $icon;
- }
-
- public function getChildren($pid)
- {
- {
- foreach ($this->items as $item)
- {
- if($item['pid']==$pid)
- {
- $children[] = $item;
- }
- }
- 回傳$children && is_array($children) ? $children : false;
- }
-
- public function getParent($id)
- {
- return $A. ->items[$this->items[$id]['pid']];
- }
-
- public function show($pid)
- {
- $children = $this- >getChildren($pid);
- if(!$children ) return false;
- foreach ($children as $child)
- {
- $this->ret.='
'; - $this->ret.='
'.$this->icon[$child['level']].$child['name'].' | ';
- $ this->ret.='刪除; 加 修改';
- $this->ret. ='
';
- $this->show($child['id']);
- }
- }
-
-
- }
-
- $items = array(
- array('id'=>1 , 'name'=>'湖北', 'pid'=>0, 'level'=>0),
- array('id'= >2 , 'name'=>'武漢', 'pid'=>1, 'level'=>1),
- array('id'=>3 , 'name'=>'孝感', 'pid '=>1, 'level'=>1),
- array('id'=>4 , 'name'=>'廣東', 'pid'=>0, 'level'=>0),
- array('id'=>5 , 'name'=>'廣州', 'pid'=>4, 'level '=>1),
- array('id'=>6 , 'name' =>'深圳', 'pid'=>4, 'level'=>1),
- array('id'=>7 , 'name'=>'東莞', 'pid'=>4, ' level'=>1),
- array('id'=>8 , 'name'=>'宜昌', 'pid'=>1, 'level'=>1),
- array('id '=>9 , 'name'=>'雲夢' , 'pid'=>3, 'level'=>2),
- array('id'=>10 , 'name'=>'南山區', 'pid'=>6, 'level'=>2),
- array('id'=>11 , 'name'=>'寶安全', 'pid'=>6, 'level'= >2),
- array('id'=>12 , 'name'=>'倒店', 'pid'=>9, 'level'=>3),
- array('id'= >13 , 'name'=>'羅範大隊', 'pid'=>12, 'level'=>4),
- array('id'=>14 , 'name'=>'下範存', 'pid'=>13, 'level'=>5),
- );
-
- $tree = new Lib_Tree($items);
- $ tree->show(0);
- echo $tree->ret;
-
- ?>
-
- 複製程式碼
|