澳门新葡萄京官网注册自己写的几个常用到的函数

?php/****计数器(v1.1.0)**Version:1.1.0*Author:村长*PHP:4.3**/classCounter{var$CountFolder;var$countType;var$showtype;var$imgArray;functionCounter(){$this-countType=0;$this-showtype=GetCountByDay;$this-imgArray=array();$this-MakeCounter();}functionMakeCounter(){$this-CountFolder=log/.date(Ymd);if(!is_dir($this-CountFolder)){mkdir($this-CountFolder,0777);}$this-_DoCount();}//Savedatafilefunction_DoCount(){$file=date(Ymd).ip2long($_SERVER[‘REMOTE_ADDR’])..txt;if(!file_exists($this-CountFolder./.$file)){$fp=@fopen($this-CountFolder./.$file,wb+);//这里可以保存其他需要的日志记录@fputs($fp,$_SERVER[‘REMOTE_ADDR’]);@fclose($fp);}}//获得指定时间的统计数据//默认为1天0为取所有functionGetCountByDay($day=1){//今日的时间戳$today=strtotime(date(Y-m-d));$all=0;for($i=$day;$i=0;$i–){$tsp=$today-(86400*$i);$folder=log/.date(Ymd,$tsp)./*.txt;$ta=glob($folder);$all=$all+count($ta);}return$all;}functionGetCountUseImg($day=1){$allnumber=$this-GetCountByDay($day);for($i=0;$istrlen($allnumber);$i++){$idx=substr($allnumber,$i,1);$returnVal.=sprintf(imgsrc=/%s/,$this-imgArray[$idx]);}return$returnVal;}functionSetStyle($style=”,$ext=gif){$imgprefix=img/;$folder=$imgprefix.$style;foreach(range(0,9)as$num){$this-imgArray[$num]=$folder./.$num..$ext;}if(is_dir($folder)){$this-showtype=GetCountUseImg;}else{$this-showtype=GetCountByDay;}}functionShow($day=0){return$this-{$this-showtype}($day);}}//实例计数器对象$counter=newCounter();//采用sports98样式显示,告知默认扩展名为png格式$counter-SetStyle(sports98,png);echobr/今日访问:.$counter-Show(0);//采用S1样式显示,默认扩展名为gif$counter-SetStyle(s1);echobr/近3日访问:.$counter-Show(2);//—-只显示文字的计数器$counter-SetStyle(-);echobr/近3日访问:.$counter-Show(2);?

<?php

      /*

       *   生成指定数量和指定字符串生成随机字符串

       *   @param int $len 获取随机字符的个数

       *   @param string $range 指定在该字符串中获取随机字符

      */

      function randomString($len,$range=”){

           if($range == ”){

                 $str =
‘0123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ’;

           }else{

                 $str = $range;

           }

           $rand_str = ”;

           for($i=0;$i<$len;$i++){

                 $rand_str .= $str[rand(0,strlen($str)-1)];

           }

           return $rand_str;

      }

      /*

       * 遍历文件夹

       *澳门新葡萄京官网注册 , @param string $path 路径

      */

      function getListDir($path){//可尝试添加按深度获取

           $file = array();

           $dir = dir($path);

           while($handle = $dir->read()){

                 if($handle != ‘.’ && $handle != ‘..’){

                      if(is_dir($dir->path.’\’.$handle)){

                            $file[$handle] =
getListDir($dir->path.’\’.$handle);

                      }else{

                            $file[] = $handle;

                      }

                 }

                 $y++;

           }

           return $file;

      }

      /*

       * 获取用户的ip地址    

      */

      function getIp(){

           $ip = ”;

           if(isset($_SERVER[‘HTTP_CLIENT_IP’])){

                 $ip = $_SERVER[‘HTTP_CLIENT_IP’];

           }elseif(isset($_SERVER[‘HTTP_X_FORWARDED_FOR’])){

                 $ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];

           }else{

                 $ip = $_SERVER[‘REMOTE_ADDR’];

           }

           return $ip;

      }

 

      /*

       * 取后缀的方法有很多,这只是其中一种

       * @param string $filename 文件名

      */

      function getExt($filename){

           $arr = explode(‘.’,$filename);

           $ext = $arr[count($arr)-1];

           return $ext;

      }

 

 

      /*

       * 记录日志(这个和老版本的shopNc的记录方式相同)

       * @param string $txt 待写入的日志内容

       * @param string $base_path 存放日志文件的路径

      */

      function log($txt,$base_path){

           header(“Content-type:text/html; charset=utf-8”);

           if(isset($base_path)){

                 $path = $base_path;

           }else{

                 $path = dirname(__FILE__);

           }

           $filename = data(“Y-m-d”).’.log’;

           $filepath = $path.’\’.$filename;

           $content = data(“Y-m-d H:i:s”).’:’.$txt.PHP_EOL;

           if(file_put_contents($filepath,$content,FILE_APPEND)){

                 return true;

           }else{

                 return false;

           }

      }

     

   

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注