封装PHP验证码类 (2010-03-05)

PHP验证码是很常见的应用
下面是一段封装好的类库
方便以后使用
 

总结一下核心函数:
imagecreate 创建图像;
imagecolorallocate  为图像上色
imagerectangle 创建矩形
imagestring 画字符串
imageline 生成干扰线
imagejpeg 生成JPEG格式
imagedestroy 催婚图像

PHP验证码类
  1. <?php
  2. session_start();
  3. class checkImage {
  4.  
  5.     private $config;
  6.     private $im;
  7.     private $str;
  8.  
  9.     function __construct() {
  10.         $this->config['width']      = 50;
  11.         $this->config['height']     = 20;
  12.         $this->config['vcode']      = "vcode";
  13.         $this->config['type']       = "default";
  14.         $this->config['length']     = 4;
  15.         $this->config['interfere']  = 10;
  16.         $this->str['default']       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  17.         $this->str['string']        = "abcdefghijklmnopqrstuvwxyz";
  18.         $this->str['int']           = "0123456789";
  19.     }
  20.  
  21.     public function init($config=array()){
  22.         if (!empty($config) && is_array($config)){
  23.             foreach($config as $key=>$value){
  24.                 $this->config[$key] =   $value;
  25.             }
  26.         }
  27.     }
  28.  
  29.     public function create(){
  30.         if (!function_exists("imagecreate")){
  31.             return false;
  32.         }
  33.         $this->createImage();
  34.     }
  35.  
  36.     private function createImage(){
  37.         $this->im   =   imagecreate($this->config['width'],$this->config['height']);
  38.         imagecolorallocate($this->im, 255, 255, 255);
  39.  
  40.         $bordercolor=   imagecolorallocate($this->im,0,0,0);
  41.         imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);
  42.  
  43.         $this->createStr();
  44.         $vcode  =   $_SESSION[$this->config['vcode']];
  45.         $fontcolor  =   imagecolorallocate($this->im,46,46,46);
  46.         for($i=0;$i<$this->config['length'];$i++){
  47.             imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
  48.         }
  49.  
  50.         $interfere  =   $this->config['interfere'];
  51.         $interfere  =   $interfere>30?"30":$interfere;
  52.         if (!empty($interfere) && $interfere>1){
  53.             for($i=1;$i<$interfere;$i++){
  54.                 $linecolor  =   imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
  55.                 $x  =   rand(1,$this->config['width']);
  56.                 $y  =   rand(1,$this->config['height']);
  57.                 $x2 =   rand($x-10,$x+10);
  58.                 $y2 =   rand($y-10,$y+10);
  59.                 imageline($this->im,$x,$y,$x2,$y2,$linecolor);
  60.             }
  61.         }
  62.         header("Pragma:no-cachern");
  63.         header("Cache-Control:no-cachern");
  64.         header("Expires:0rn");
  65.         header("content-type:image/jpegrn");
  66.         imagejpeg($this->im);
  67.         imagedestroy($this->im);
  68.         exit;
  69.     }
  70.  
  71.     private function createStr(){
  72.         if ($this->config['type']=="int"){
  73.             for($i=1;$i<=$this->config['length'];$i++){
  74.                 $vcode  .=  rand(0,9);
  75.             }
  76.             $_SESSION[$this->config['vcode']] = $vcode;
  77.             return true;
  78.         }
  79.         $len    =   strlen($this->str[$this->config['type']]);
  80.         if (!$len){
  81.             $this->config['type'] = "default";
  82.             $this->create_str();
  83.         }
  84.         for($i=1;$i<=$this->config['length'];$i++){
  85.             $offset  =  rand(0,$len-1);
  86.             $vcode  .=  substr($this->str[$this->config['type']],$offset,1);
  87.         }
  88.         $_SESSION[$this->config['vcode']] = $vcode;
  89.         return true;
  90.     }
  91.  
  92. }
  93.  
  94. $v = new checkImage();
  95. //$config['width']  =   50;         //验证码宽
  96. //$config['height'] =   20;         //验证码高
  97. //$config['vcode']  =   "vcode";    //检查验证码时用的SESSION
  98. //$config['type']   =   "int";  //验证码展示的类型default:大写字母,string:小写字母,int:数字
  99. //$config['length'] =   2;          //验证码长度
  100. //$config['interfere']= 10;         //干扰线强度,范围为1-30,0或空为不起用干扰线
  101. //$v->init($config);    //配置
  102. $v->create();
  103.  
  104. ?>
  105.  
  106. <SCRIPT LANGUAGE="JavaScript">
  107.     <!--
  108.         function reloadcode(){
  109.          var d = new Date();
  110.          document.getElementById('safecode').src="./vcode.php?t="+d.toTimeString()
  111.         }
  112.     //-->
  113. </SCRIPT>
  114.  
  115. <img src="vcode.php" id="safecode" onclick="reloadcode()">

 

点击查看原文阅读(20) | 评论(0) | 分类:PHP

2010年元宵节 (2010-02-28)

今天元宵节了,
早上吃的元宵
白天去排了一天的队,人真多
晚上大街上都是放鞭炮的,
看着家长抱着孩子,
小孩手里拿着烟火棒
觉得现在的小孩真是幸福
总之比我小时候幸福多了
夜晚的景色很美
今天还下了小雪
我顺手拍了几张
元宵节快乐!

2010年元宵节-1 

2010年元宵节-2 

2010年元宵节-3 

2010年元宵节-4

点击查看原文阅读(35) | 评论(0) | 分类:我的日记

最简单的PHP模板引擎 (2010-02-21)

模板类都是用是现成的,没有自己写过,杯具了!
今天自己写一下PHP模板引擎;
这是个最精简的PHP模板类;
模板标签采用纯天然原生态PHP语法
<?=$test?>使用这种原生形式比较快
因为PHP本身就是个很好的模板引擎
瞎写的玩的,反正核心原理就是替换变量
这是0.0001beta版,哈
过两天做个完整的

template.class.php
  1. <?php
  2. class Templates {
  3.  
  4.     var $vars;
  5.     var $path;
  6.  
  7.     public function __construct($path = null) {
  8.         $this->path = $path;
  9.     }
  10.  
  11.     public function assign($name, $value) {
  12.         $this->vars[$name] = $value;
  13.     }
  14.  
  15.     public function display($file) {
  16.         extract($this->vars);
  17.         ob_start();
  18.         include($this->path.$file.'.tpl.php');
  19.         $contents = ob_get_contents();
  20.         ob_end_clean();
  21.         return $contents;
  22.     }
  23.  
  24. }
  25.  
  26. /*test*/
  27. $template_url = './tp/';
  28. $name = 'Test Tempalte';
  29. $tpl = new Templates($template_url);
  30.  
  31. $tpl->assign('title', $name);
  32. $tpl->assign('user_list', fetch_user_list());
  33.  
  34. echo $tpl->display('test');
  35. ?>

 

 

点击查看原文阅读(114) | 评论(0) | 分类:PHP

大年初七,在家无聊,写了个mysql操作类 (2010-02-20)

在家无聊,写了个简单的DB类,方便以后CURD;
以前用公司现成的DB类,现在自己写个;
支持Mysqli,高手见笑,本人还是菜鸟 ;
 

代码如下
  1. <?php
  2.  
  3. class MysqlDB {
  4.  
  5.     private $hostname;
  6.     private $username;
  7.     private $password;
  8.     private $db;
  9.  
  10.     public function __construct() {
  11.         $num_args = func_num_args();
  12.         if($num_args > 0) {
  13.             $args = func_get_args();
  14.             $this->host = $args[0];
  15.             $this->user = $args[1];
  16.             $this->pass = $args[2];
  17.         }
  18.         $this->content();
  19.     }
  20.  
  21.     private function mysqli_installed(){
  22.         if (function_exists ("mysqli_connect")){
  23.             return true;
  24.         } else {
  25.             return false;
  26.         }
  27.     }
  28.  
  29.     private function content() {
  30.         try{
  31.          if($this->mysqli_installed()) {
  32.              if(!$this->db = new mysqli($this->host,$this->user,$this->pass)) {
  33.                  $exceptionstring = "Error connection to database: <br />";
  34.                  $exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error();
  35.                  throw new exception ($exceptionstring);
  36.              }
  37.          }else{
  38.             if(!$this->db = new mysql_connect($this->host,$this->user,$this->pass)) {
  39.                 $exceptionstring = "Error connection to database: <br />";
  40.                 $exceptionstring .= mysql_errno() . ": " . mysql_error();
  41.                 throw new exception ($exceptionstring);
  42.             }
  43.          }
  44.         } catch (exception $e) {
  45.             echo $e->getmessage();
  46.         }
  47.     }
  48.  
  49.     public function select_db($db_name) {
  50.         try{
  51.             if($this->mysqli_installed() ) {
  52.                 if(!$this->db->select_db($db_name)) {
  53.                     $exceptionstring = "Error opening database: $db_name: <br />";
  54.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  55.                     throw new exception ($exceptionstring);
  56.                 }
  57.             }else{
  58.                 if(!mysql_select_db($db_name,$this->db)) {
  59.                     $exceptionstring = "Error opening database: $db_name: <br />";
  60.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  61.                     throw new exception ($exceptionstring);
  62.                 }
  63.             }
  64.         } catch (exception $e) {
  65.             echo $e->getmessage();
  66.         }
  67.     }
  68.  
  69.     public function execute($query) {
  70.         try{
  71.             if($this->mysqli_installed()) {
  72.                 if(!$this->db->query($query)) {
  73.                     $exceptionstring = "Error performing query: $query: <br />";
  74.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  75.                     throw new exception ($exceptionstring);
  76.                 }
  77.             }else{
  78.                 if(!mysql_query($query,$this->db)) {
  79.                     $exceptionstring = "Error performing query: $query: <br />";
  80.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  81.                     throw new exception ($exceptionstring);
  82.                 }else{
  83.                     echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
  84.                 }
  85.             }
  86.         } catch (exception $e) {
  87.             echo $e->getmessage();
  88.         }
  89.     }
  90.  
  91.     public function get_rows ($query){
  92.         try {
  93.             if ($this->mysqli_installed()){
  94.                 if ($result = $this->db->query ($query)){
  95.                     $returnarr = array ();
  96.                     while ($adata = $result->fetch_array ()){
  97.                         $returnarr = array_merge ($returnarr,$adata);
  98.                     }
  99.                     return $returnarr;
  100.                 } else {
  101.                     $exceptionstring = "Error performing query: $query: <br />";
  102.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  103.                     throw new exception ($exceptionstring);
  104.                 }
  105.             } else {
  106.                 if (!$aquery = mysql_query ($query)){
  107.                     $exceptionstring = "Error performing query: $query: <br />";
  108.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  109.                     throw new exception ($exceptionstring);
  110.                 } else {
  111.                     $returnarr = array ();
  112.                     while ($adata = mysql_fetch_array ($aquery)){
  113.                         $returnarr = array_merge ($returnarr,$adata);
  114.                     }
  115.                     return $returnarr;
  116.                 }
  117.             }
  118.         } catch (exception $e) {
  119.             echo $e->getmessage();
  120.         }
  121.     }
  122.  
  123.     public function affected_rows() {
  124.         if($this->mysqli_installed()) {
  125.             return $this->db->affected_rows;
  126.         }else{
  127.             return mysql_affected_rows ($this->db);
  128.         }
  129.     }
  130.  
  131.     public function insert_id() {
  132.         if($this->mysqli_installed()) {
  133.             return $this->db->insert_id;
  134.         }else{
  135.             return mysql_insert_id ($this->db);
  136.         }
  137.     }
  138.  
  139.     public function escape($str){
  140.         return trim(mysql_escape_string($str));
  141.     }
  142.  
  143.     public function __destruct() {
  144.         try {
  145.             if ($this->mysqli_installed()){
  146.                 if (!$this->db->close()){
  147.                     $exceptionstring = "Error closing connection: <br />";
  148.                     $exceptionstring .= $this->db->errno . ": " . $this->db->error;
  149.                     throw new exception ($exceptionstring);
  150.                 }
  151.             } else {
  152.                 if (!mysql_close ($this->db)){
  153.                     $exceptionstring = "Error closing connection: <br />";
  154.                     $exceptionstring .= mysql_errno() . ": " . mysql_error();
  155.                     throw new exception ($exceptionstring);
  156.                 }
  157.             }
  158.         } catch (exception $e) {
  159.             echo $e->getmessage();
  160.         }
  161.     }
  162.  
  163. }
  164.  
  165.  
  166. /*test*/
  167. $mysql = new MysqlDB('localhost','root','');
  168.  
  169. $mysql->select_db('test');
  170.  
  171. $update = $mysql->execute ("UPDATE test SET usereMail='test@test.com' WHERE id='1'");
  172.  
  173. $insert = $mysql->execute ("INSERT INTO test SET usereMail = 'test@test.com'");
  174.  
  175. $insert_id = $mysql->insert_id($insert);
  176.  
  177. $test = $mysql->get_rows("SELECT * FROM `test` WHERE 1");
  178.  
  179. $affected_rows = $mysql->affected_rows($insert);
  180.  
  181.  
  182. ?>

点击查看原文阅读(125) | 评论(1) | 分类:PHP

大年初二 (2010-02-15)

今天大年初二了
全家人去全聚德吃饭刚回来
给我的感觉是
今年突然感觉爸妈都老许多
白头发更多了
我还沉浸在童年呢?
现在都是我给亲戚小孩压岁钱了
看来我也越来越大了
家里很多事情该我撑起来
不能再像小孩了!......

点击查看原文阅读(136) | 评论(0) | 分类:我的日记

(2010-02-10)

我想要有个家
一个不需要华丽的地方
在我疲倦的时候 我会想到它
一个不需要多大的地方
在我受惊吓的时候 我才不会害怕
谁不会想要家
可是就有人没有它
脸上流着眼泪 只能自己轻轻擦
我好羡慕他
受伤后可以回家
而我只能孤单地孤单地 寻找我的家
虽然我不曾 有温暖的家
但是我一样 渐渐的长大
只要心中充满爱 就会被关怀
无法埋怨谁 一切只能靠自己
虽然你有家 什么也不缺
为何看不见你露出笑脸
相同的年纪 不同的心灵
让我拥有一个家

今天放假第一天
和哥们出去玩了
无意中听到了这首歌
自己也不知道是什么心情
以前听这首歌也没什么感觉
不知道为什么
我把歌词贴上来了
心想着未来还远吗
一个温馨的家对于我们是多么重要
谁不想自立门户呢
我要努力攒钱了
为以后打基础
那时候也老了 =^_^= =^_^=
 

点击查看原文阅读(162) | 评论(2) | 分类:我的日记

放血 (2010-02-03)

这两天给自己买了点东西
可放我血了
不知道买的值不值
就当投资吧, hehe

另外这两天比较背
原因是在公司玩游戏
互相给对方选人
都不按套路打
呵呵 希望明天不是我、
因为输的人要去买麦当劳
售货员大大都认识我了
每次都买这么多..
为麦当劳事业做出贡献

点击查看原文阅读(210) | 评论(1) | 分类:我的日记

一行代码兼容IE.OP.FF.SF的收藏夹JS (2010-01-29)

网上找了一下
加入收藏夹 js 兼容Firefox 和IE
就一句:
 

代码如下
  1. <a href="http://www.nonb.cn" onclick="window.external.addFavorite(this.href,this.title);return false;" title='写点寂寞' rel="sidebar">加入收藏</a>

点击查看原文阅读(212) | 评论(0) | 分类:我的日记

我的追求是什么 (2010-01-20)

我的追求是什么.这个问题我想了很久
真的与许多问题有关。
先是一个和睦的家庭,父母身体健康
再是顺利完成学业,找一份自己满意的工作,
再有几个关系铁的哥们,能够共同奋斗的哥们
然后得到一份真正属于自己的爱情,
不需要对方有多么的漂亮,多么的优秀
只要另一半适合你, 贴心照顾,互相扶持。
并且能够忍受你身上的缺点
无论发生什么事,都会共同面对
我的追求就是这样吧,
说到底我也只是一个平凡的人
有着喜怒哀乐,有着酸甜苦辣

点击查看原文阅读(283) | 评论(2) | 分类:我的日记

公交地铁 (2010-01-16)

今天出去了一天
就像是公交地铁一日游
跨越北京海淀,丰台,宣武,西城
最后去了趟菜百,
看到了所有人都喜欢的东西 呵呵

点击查看原文阅读(266) | 评论(0) | 分类:我的日记