脱裤子放屁典范程序

作者 : admin 于 2009年06月05日, 10:25:48
2009
06-5

最近搞一个单点登录的东东,研究了下康盛的ucenterhome产品,在研究cookie的时候发现这么一段处理程序:

  1. $prelength = strlen($_SC['cookiepre']);
  2. foreach($_COOKIE as $key => $val) {
  3. if(substr($key, 0, $prelength) == $_SC['cookiepre']) {
  4. $_SCOOKIE[(substr($key, $prelength))] = empty($magic_quote) ? saddslashes($val) : $val;
  5. }
  6. }

其目的是把合法的cookie拿出来防到一个全局变量里去用。

cookie本身就是一个挺好的全局变量,而康盛又把这个变量拿出来去用,为什么?

或者,为了方便管理变量,但判断函数判断自定义全局变量跟判断cookie全局变量成本相同;

或者,为了让开发者必须彻底明白业务逻辑才能修改,提高竞争对手抄袭门槛或者提高二次开发门槛而给项目部带来更高的收入?

这样的代码里边很多,也许只有他们自己才能了解最终目的。

xml与array互转函数

作者 : admin 于 2009年05月31日, 18:42:11
2009
05-31

突然发现写程序不像原来思路那么清晰了,一个递归居然想了半天,不过还是凑合着写了一个简单的xml与array互转的函数,来实现跟其他系统的对接。

原来一直用的是拼凑的方式生成xml,这次使用了DOMDocument对象。不知道这个类是否有非法字符的问题,待测试一下便知。

  1. class AXML
  2. {
  3. var $dom = '';
  4. #    array to xml       ###########################################
  5.     #array to xml
  6. function array2xml( $array )
  7. {
  8. $this->dom = new DOMDocument('1.0', 'utf-8');
  9. $this->dom->formatOutput = true;
  10. $this->_array( array( 'root' => $array ) , $this->dom );
  11. echo $this->dom->saveXML();
  12. }
  13.  
  14. #array to dom
  15. function _array( $array = array() , $dom )
  16. {
  17. $i = 0;
  18. foreach( $array as $key => $value )
  19. {
  20. if( is_array( $value ) )
  21. {
  22. //遍历
  23. $node[$i] = $this->dom->createElement( $key );
  24. $dom->appendChild( $node[$i] );
  25. $this->_array( $value , $node[$i] );
  26. }
  27. else
  28. {
  29. $node[$i] = $this->dom->createElement( $key , $value );
  30. $dom->appendChild( $node[$i] );
  31. }
  32. $i++;
  33. }
  34. }
  35. #   xml  to array       ###########################################
  36. #xml to array
  37. function xml2array( $xmls )
  38. {
  39. $array = array();
  40. $this->dom = new SimpleXMLElement( $xmls );
  41. print_r( $this->_object( $this->dom ) );
  42. }
  43. #object to array
  44. function _object( $object )
  45. {
  46. $array = array();
  47. $t = get_object_vars( $object );
  48. $i = 0;
  49. foreach( $t as $key => $value )
  50. {
  51. if(is_object($value))
  52. {
  53. $array[$key] = $this->_object( $value );
  54. }
  55. else
  56. {
  57. $array[$key] = $value;
  58. }
  59. $i++;
  60. }
  61. return $array;
  62. }
  63. }
  64. $xml = new AXML();
  65. $xml->array2xml( $array );
  66. $xml->xml2array( $xml_string );

Linux下APACHE MYSQL PHP FCgid Suexec 配置文档V1.0

作者 : admin 于 2009年05月22日, 11:14:12
2009
05-22

文档版本:V1.0

启动撰写时间: 2009年05月20日

目的:全面详细介绍LAMP fastcgi方式配置细节,基于之前的自动配置脚本,目的是把更多的细节转达给大家。

需要软件源码:

CentOS4.7

http://centos.ustc.edu.cn/centos/4.7/isos/i386/CentOS-4.7.ServerCD-i386.iso

http://centos.ustc.edu.cn/centos/4.7/isos/x86_64/CentOS-4.7.ServerCD-x86_64.iso

Apache-2.2.9

http://archive.apache.org/dist/httpd/httpd-2.2.9.tar.gz

MYSQL-5.2.6

http://downloads.mysql.com/archives/mysql-5.0/mysql-5.0.22.tar.gz

PHP-5.2.6

http://museum.php.net/php5/php-5.2.6.tar.gz

FCGID

http://ncu.dl.sourceforge.net/sourceforge/mod-fcgid/mod_fcgid.2.2.tgz

安装:

第一步:Linux系统安装,同时可以参照我原来的文档

http://www.sunboyu.cn/2008/06/13/centos5%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97%EF%BC%88%E7%AE%80%E5%8D%95%E7%AF%87%EF%BC%89.shtml

阅读全部 »

Smarty分页类修正版

作者 : admin 于 2009年05月08日, 03:59:49
2009
05-8

原来写的分页类有问题,经过排查,发现一个变量用错了,修改之

效果,很简单,黑白配,有心的人可以自己修改一下风格,欢迎共享

e69caae591bde5908d-1

PHP部分

  1. < ?php
  2. /*  Page.Class.php
  3.  *  Page
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2009 05 07  sunboyu@gmail.com
  9.  *  Demo
  10. $page = new Page( 1 , 'v_user' , '*' , '' );
  11. $rs = $page->__getlist();
  12. $smarty->assign("page",$page->__getpagelist());
  13.  */
  14.  class Page
  15.  {
  16. public $count;      #结果总数
  17. public $page;       #当前页
  18. public $pagesize;   #每页结果数
  19. public $pagecount;  #翻页数
  20. public $baseurl;    #url
  21. public $result;     #结果数组集
  22. public $pagelist;   #每翻页数
  23. public $db;         #数据库连接
  24. public $table;      #要查询的表
  25. public $fileds;     #要返回的字段
  26. public $where;      #where条件
  27.  
  28. #构造函数,初始化变量
  29. function __construct( $page , $table , $fields = '*' , $where = false , $baseurl = false )
  30. {
  31.      global $_CFG,$db;
  32. $this->db        = $db;
  33. $this->table     = $table;
  34. $this->page      = isset($page) ? intval($page) : 1;
  35. $this->fileds    = $fields;
  36. $this->pagesize  = $_CFG['pagesize'];
  37. $this->baseurl   = ($baseurl!=false) ? $baseurl : $this->__geturl();
  38. $this->pagelist  = $_CFG['pagelist'];
  39. $this->where     = $where;
  40. }
  41.  
  42. #获得当前url
  43. function __geturl()
  44. {
  45. parse_str($_SERVER['QUERY_STRING'],$str);
  46. if(isset($str['page']))
  47. {
  48. unset($str['page']);
  49. }
  50. return count($str) > 0 ? "?".http_build_query($str) : "?";
  51. }
  52.  
  53. #获得记录集
  54. function __getlist()
  55. {
  56.      #获得count记录
  57. $sql = sprintf("SELECT COUNT(*) AS table_count FROM %s %s",$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''));
  58. $rs = $this->db->fetch( $sql );
  59. $this->count = $rs['table_count'];
  60.      $offset = ($this->page-1)*$this->pagesize-1;
  61.      $sql = sprintf("SELECT %s FROM %s %s LIMIT %d,%d",$this->fileds,$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''),$this->pagesize*($this->page-1),$this->pagesize);
  62. #echo $sql;
  63.          $list = $this->db->fetchAll( $sql );
  64. return $list;
  65. }
  66.  
  67. #获得分页列表
  68. function __getpagelist()
  69. {
  70. $this->result['count'] = $this->count;
  71. $this->result['page'] = $this->page;
  72. $this->result['pagesize'] = $this->pagesize;
  73. $this->result['pagecount'] = ceil($this->count/$this->pagesize);
  74. if($this->result['pagecount']< =1) //只有一页以下
  75. {
  76. $this->result['pagelist'] = 0;
  77. }
  78. else //一页以上
  79. {
  80. #前一页,第一页的算法
  81. $this->result['first'] = ($this->page == 1) ? 0 : 1;
  82. $this->result['pre'] = ($this->page == 1) ? 0 : 1;
  83. #后一页,最后一页的算法
  84. $this->result['next'] = ($this->page == $this->result['pagecount'] ) ? 0 : 1;
  85. $this->result['last'] = ($this->page == $this->result['pagecount'] ) ? 0 : 1;
  86.  
  87. #起始
  88. $pagearray = array();
  89. $start = floor(($this->page-1)/10)*10+1;
  90. for($i=0;$i&lt;10;$i++)
  91. {
  92.      if( ($start+$i) < = $this->result['pagecount'])
  93. {
  94.      $pagearray[$i]['page'] = $start+$i;
  95. }
  96. if( ($start+$i) != $this->page )
  97. {
  98.      $pagearray[$i]['link'] = 1;
  99. }
  100. }
  101. #分页导航列表
  102. $this->result['pagelist'] = $pagearray;
  103. $this->result['baseurl'] = $this->baseurl;
  104. }
  105. return $this->result;
  106. }
  107. }
  108. ?>

模板部分

  1. <div id="page">
  2. <table>
  3.     <tr>
  4.     <td>
  5. 共{{$page.count}}条数据 每页{{$page.pagesize}}条  共{{$page.pagecount}}页 当前第{{$page.page}}页
  6. </td>
  7. <td>
  8. {{if $page.pagecount>1}}
  9. {{if $page.first eq 1}}
  10. <a href="{{$page.baseurl}}">首页</a>
  11. {{else}}
  12. 首页
  13. {{/if}}
  14. {{if $page.pre eq 1}}
  15. <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
  16. {{else}}
  17. 上一页
  18. {{/if}}
  19. {{foreach from=$page.pagelist item=vols}}
  20.     {{if ($vols.page > 0 ) && ($vols.page < = $page.pagecount) }}
  21. {{if $vols.link eq 1}}
  22. <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]
  23. {{else}}
  24. [{{$vols.page}}]
  25. {{/if}}
  26. {{/if}}
  27. {{/foreach}}
  28. {{if $page.next eq 1}}
  29. <a href="{{$page.baseurl|default:"?"}}&page={{$page.page+1}}">下一页</a>
  30. {{else}}
  31. 下一页
  32. {{/if}}
  33. {{if $page.last eq 1}}
  34. <a href="{{$page.baseurl|default:"?"}}&page={{$page.pagecount}}">尾页</a>
  35. {{else}}
  36. 尾页
  37. {{/if}}
  38. {{/if}}
  39. </td>
  40. </tr>
  41. </table>
  42. </div>

补充一个demo

  1. #PHP部分
  2. $page = new Page( $page , $this->area_table , $fields = '*' , $where);
  3. $result['rs'] = $page->__getlist();
  4. $result['page'] = $page->__getpagelist();
  5. $smarty->assign("list",$result['rs']);
  6. $smarty->assign("page",$result['page']);
  7. $smarty->display('list.tpl');
  8. #模板里边只要引用这个分页模板即可
  9. {{include file=$smarty.const.Tpl|cat:"/Page.tpl"}}

我的框架依然有人记得

作者 : admin 于 2009年05月05日, 19:59:09
2009
05-5

去年尝试开发了一个框架,开发了一半。或者说第一阶段开发完成。

为了开发这个框架,阅读了大量框架代码,框架思想,然后精心去调试了自己的框架。

在框架完成后,我并没有去使用。

框架的初衷是为了性能跟开发规范,而我在这个框架下兼职无法写代码,无法发挥。自从学写PHP,我从来没受什么规矩约束过,向来是自由度很高。而规则则是多年编程留下的一些经验。后来发现我的风格实在无法用框架来约束,所以放弃使用这个框架,依然按照自己的风格进行开发。

但反思,为什么有很多的框架在项目中应用-那就是规范。不成规矩,不成方圆。尤其在团队合作开发过程中,团队利益就高于个体利益。如果每个人能损失一点个人利益,那团队就可能拥有至高的利益。

因此,我又尝试开发了一套闭源自用的框架。其既集成了框架的一些模块化,规范化的理念,又适合程序员在小范围内自由发挥。后来我又加上了一些管理功能,通用模块系统,现在我可以在这个上边快速开发,又可以很轻松得把模块拆离重组。

内心我并没有把他当作框架,叫做程序“模子”会更好,感谢lamp群友给这么个名字。

PHP DeZend真的很爽

作者 : admin 于 2009年05月02日, 00:19:03
2009
05-2

突然在网上发现一套我渴望已久的IDC管理软件,下载下来,除用vb之类的加密外,其源代码是用zend加密过的PHP代码。大喜,网上找到这个工具,然后迅速反接后,看到所有源代码。开始研究。

PHP DeZend

几个画蛇添足的函数,出自我手

作者 : admin 于 2009年03月18日, 01:20:14
2009
03-18

翻看自己以前的代码,发现自己的一些搞笑的函数,列一个

  1. function func_arrtoQuerystring($array)             #其实就是http_build_query
  2. {
  3.     $string = '?';
  4.     foreach($array as $key=>$value)
  5. {
  6.     $string .= $key.",".$value.";";
  7. }
  8. $string = substr($string,0,substr($string)-1);
  9. $string .= ".html";
  10. return $string;
  11. }

回头多找几个这样的,自勉

file_get_contents函数使用post方法

作者 : admin 于 2009年03月16日, 12:27:44
2009
03-16

以前总用socket的方式发送接收http的包,结果收到的包也有一堆http的协议头信息。
处理这些信息还挺费劲的。

然后想到了file_get_contents($url)方法,可以得到纯净的http包的正文。但这种方式默认是get的方式,后查手册和搜索,得到了post的方法。

这种方式基本跟socket的方式相同,包头构建好即可。

  1. $array = array ('a' => 'b','c'=>'d');
  2. $url= http_build_query($array );
  3. $postdate = array (
  4.     'http' => array (
  5.         'method' => 'POST',
  6.         'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
  7.                    "Content-Length: " . strlen($url) . "\r\n",
  8.         'content' => $url
  9.     ),
  10. );
  11. $postcontent = stream_context_create($postdate );
  12. $return= file_get_contents('http://www.sunboyu.cn', false, $postcontent ); 
  13. echo $return;

解决了该死的权限问题,是否真的有效

作者 : admin 于 2009年03月12日, 23:43:34
2009
03-12

我的apache+php权限是配置的最为严格的,当然,在用的时候难免伴随着混乱的账户情况,linux的权限机制也是很让人头疼的,终于,还是在风平浪静种碰到了麻烦。

我不认为PHP很强,因为它只是一个面向Web的脚本语言,而PHP的开发者却赋予了它太多,让人去用,有人也滥用。

当然,一门语言能解决N多问题是好的,比如汇编,C,但终究PHP有它跨不过的坎。毕竟它只是web脚本语言。

权限出现很大的问题,至今没有搞透,说白了对linux还是一知半解,使用python写了个第三方的东西,很完美得跳跃了权限的问题。至于效率,python肯定要比php强的,起码PHP作为服务器端程序运行,PHP还没有线程和进程的控制(一直没有发现),python有完善的线程进程的库。在权限管理上,python没细看,PHP在linux下有posix函数库,我一直也没有用过。

在没有更好的解决方案前,我依然用PHP做服务器端程序,python作为一些补充。也许,全部切换过去。

SQLite牛刀小试

作者 : admin 于 2009年03月07日, 02:07:28
2009
03-7

这玩意据说跟bdb一样,嵌入式数据库,正好嵌到了PHP上边,因为写框架,框架里新添加了一个功能,就是管理模块的一个模块,本想用mysql来做,又一想,用mysql,那迁移起来可麻烦了,不运行一个install就没法部署了,不如把功能直接镶嵌,直接使用。
SQLite跟mysql一样,是关系型数据库,而且功能都类似,我直接使用原来的接口,写了一个数据抽象层,直接挂接到原框架里,很好用。
SQLite管理,我还找了两个工具,一个桌面版的,类似mysql front ,一个web版的,看来就像phpmyadmin了。

413 Request Entity Too Large

——————————————————————————–

nginx/0.5.34

FUCK Nginx HOST

那个sqlitemanager http://www.sqlitemanager.org/

代码如下,因为使用不多,临时用一下,所以细节没怎么处理,待出问题的时候再来修改。

  1. < ?php
  2. /*  DB.SQLite.Class.php
  3.  *  SQLite
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2009 03 07  sunboyu@gmail.com
  9.  *  Demo
  10.  */
  11. require_once(ROOT."/Include/Class/DB.InterFace.php");
  12. class SQLite implements DateBaseConnect
  13. {
  14. #连接标识
  15. public $handle = false;
  16. #结果标识
  17. public $query;
  18. #查询次数
  19. public $exetime;
  20. #数据库连接
  21.     public function Connect( $argvs )
  22. {
  23. $this->handle = @sqlite_open( $argvs['hostname'].'/'.$argvs['datebase'] , $argvs['mode'] , $sqliteerror );
  24. if(!$this->handle)
  25. {
  26. die("Can't connect to the datebase ".$argvs['datebase']);
  27. }
  28. $this->exetime = 0;
  29. }
  30. #使用数据库
  31. public function selectDateBase( $datebase )
  32. {
  33. #此方法无用
  34. return true;
  35. }
  36. #执行一个查询
  37. public function query( $sql )
  38. {
  39.         #echo $sql;
  40. $this->query = sqlite_query( $sql , $this->handle ) or die("query error".sqlite_last_error( $this->handle ));
  41. $this->exetime++;
  42. return true;
  43. }
  44. #取得一行
  45. public function fetch( $sql )
  46. {
  47. $this->query( $sql );
  48. while( $row = mysql_fetch_array( $this->query, MYSQL_ASSOC ) )
  49. {
  50. return $row;
  51.     }
  52. return false;
  53. }
  54. #取得所有
  55. public function fetchAll( $sql )
  56. {
  57. $this->query( $sql );
  58. return sqlite_fetch_all( $this->query );
  59. }
  60. #取得影响行数
  61. public function affectedRow()
  62. {
  63. return true;
  64. }
  65. #取得结果行数
  66. public function recordCount()
  67. {
  68. return sqlite_num_rows( $this->query );
  69. }
  70. #取得上次插入ID
  71. public function insertID()
  72. {
  73. return false;
  74. }
  75. #释放资源
  76. public function close()
  77. {
  78. unset( $this->handle );
  79. }
  80. #析构函数
  81. /*
  82. function __destruct()
  83. {
  84. $this->close();
  85. }
  86. */
  87. }
  88. ?>

 Page 4 of 10  « First  ... « 2  3  4  5  6 » ...  Last »