php class的申明与使用方法
<?php
/*
* Explorer! 函数库
* 编写日期:2008-06-29
* 最后更新:2008-07-18 2:08
*
*/
class System{//系统部分
function usr_level($name){
$SQL = new MySQL();
$SQL->Query("SELECT `level` FROM `members` WHERE `username` = '$name';");
$SQL->NextRecord();
$TMP = $SQL->GetRecord('level');
$SQL->Free();
return $TMP;
}
function channel_level($cid){
$SQL = new MySQL();
$SQL->Query("SELECT `level` FROM `channels` WHERE `id` = $id;");
$SQL->NextRecord();
$TMP = $SQL->GetRecord('id');
$SQL->Free();
return $TMP;
}
function uid2name($uid){
$SQL = new MySQL();
if($SQL->Query("SELECT `username` FROM `members` WHERE `uid` = $uid;")){
$SQL->NextRecord();
$TMP = $SQL->GetRecord('username');
$SQL->Free();
return $TMP;
}else{
return 0;
}
}
function name2uid($name){
$SQL = new MySQL();
if($SQL->Query("SELECT `uid` FROM `members` WHERE `username` = '$name';")){
$SQL->NextRecord();
$TMP = $SQL->GetRecord('uid');
$SQL->Free();
return $TMP;
}else{
return 0;
}
}
function sysinfo($Name){//获取系统信息
$SQL = new MySQL();
$SQL->Query("SELECT * FROM `sysinfo`;");
$SQL->NextRecord();
$TMP = $SQL->GetRecord($Name);
$SQL->Free();
return $TMP;
}
function find_member($name){//查找该用户(注册时需要)
$SQL = New MySQL();
$SQL->Query("SELECT * FROM `members` WHERE `username` = '$name';");
$RS = $SQL->RowS();
$SQL->Free();
if($RS)
return 1;
else
return 0;
}
function str_safe($str){//字符串安全过滤
$str = str_replace($str,";",";");
$str = str_replace($str,"'","‘");
$str = str_replace($str,"/","/");
$str = str_replace($str,"`","`");
$str = str_replace($str,"\","\");
return $str;
}
function GetMyIP()
{
if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
elseif (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
elseif (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
elseif (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "127.0.0.1";
return $ip;
}
function Version(){
return "1.0.9";
}
}
class MySQL{//数据库部分
var $DBServer = 'localhost';//服务器
var $DBName = '';//数据库名称
var $DBUser = '';//数据库用户
var $DBPass = '';//数据库密码
var $OnErrorResume = 1;//错误提示关闭
var $LinkID = 0;//连接句柄
var $QueryID = 0;//查询句柄
var $ResultS = array();//查询结果集
var $Error = '';//错误信息
function Connect($Srv = "",$Usr = "",$Pass = "",$DB = ""){//连接数据库
if($Srv == "") $Srv = $this->DBServer;
if($Usr == "") $Usr = $this->DBUser;
if($Pass == "") $Pass = $this->DBPass;
if($DB == "") $DB = $this->DBName;
if($this->LinkID == 0){
$this->LinkID = @mysql_connect($Srv,$Usr,$Pass) or die("数据库连接失败,请联系管理员修复此问题。");
}
@mysql_select_db($DB,$this->LinkID) or die("数据库选择失败,请联系管理员修复此问题。");
return $this->LinkID;
}
function Free(){//释放查询结果
@mysql_free_result($this->QueryID);
$this->QueryID = 0;
}
function RowS(){//查询到的记录总数
if(!$this->QueryID) return 0;
return @mysql_num_rows($this->QueryID);
}
function NextRecord(){//下一条记录
if(!$this->QueryID) return 0;
$this->ResultS = @mysql_fetch_array($this->QueryID);
}
function Seek($seek){
if(!$this->QueryID) return 0;
@mysql_data_seek($this->QueryID,$seek);
}
function Query($Sql){//执行查询
if($Sql == "") return 0;
if($this->LinkID == 0) $this->Connect();
if($this->QueryID) $this->Free();//释放原来查询结果
$this->QueryID = @mysql_query($Sql,$this->LinkID);
$this->Error = mysql_error($this->LinkID);
if(!$this->QueryID) exit("$Sql执行失败.");
return $this->QueryID;
}
function GetRecord($Name){
if(!$this->QueryID) return 0;
return $this->ResultS[$Name];
}
}
?>
class TEMPLATE
{
private $path = "." ; #根目录
private $var;
private $tplDir = "template"; #模板存储目录
private $tplExt = "tpl"; #模板文件的后缀名
private $tplId = 0 ; #模板的ID号
private $compileDir = "template_c"; #编译后的php文件存放目录
private $isCache=false ; #是否用缓存 (默认不启动)
private $cacheId = 1; #缓存文件ID号
private $cacheLeftTime=3600; #缓存有效期 (默认保存3600秒)
private $cacheDir = "cache"; #缓存文件存储目录
private $autoRefresh = false ; #是否自动刷新
private $pattern = array(
"/({dw:)s*includes*filename=s*"(.+..+)s*"s*(/})/i",#包含文件
"/({dw:)s*field.(.+)s*(/})/i",#局部变量
"/({dw:)s*global.(.+)s*(/})/i",#全局变量
"/({dw:)s*foreachs*(.+)s*ass*(.+)s*(/})/i",#foreach 语句
"/({dw:)s*ends*foreachs*(/})/i", #foreach 结束
"/({dw:)s*ifs*((.+))(/})/i",
"/({dw:)s*elseifs*((.+))(/})/i",
"/({dw:)s*elses*(/})/i",
"/({dw:)s*ends*ifs*(/})/i",
);
private $replacement = array(
'<?php echo $this->inc_file("\2"); ?>',
"<?php echo $\2;?>",
"<?php global $\2;n echo $\2; ?>",
"<?php foreach($\2 as $\3){ ?>",
"<?php } ?>",
"<?php if (\2) { ?>" ,
"<?php }else if(\2){ ?>",
"<?php }else{ ?>",
"<?php } ?>",
);
#构造函数
function __construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="")
{
$this->path = $path ? $path : $this->path ;
$this->tplDir = $tplDir ? $tplDir : $this->tplDir ;
$this->compileDir = $compileDir ? $compileDir : $this->compileDir ;
$this->isCache = is_bool($isCache) ? $isCache : $this->isCache ;
$this->cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime ;
$this->cacheDir = $cacheDir ? $cacheDir : $this->cacheDir ;
$this->autoRefresh = is_bool($autoRefresh) ? $autoRefresh : $this->autoRefresh ;
}
#兼容php4
function TEMPLATE($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="")
{
$this->__construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="");
}
function __get($property)
{
return $this->$property ;
}
function __set($property,$value)
{
return $this->$property = $value ;
}
#给模板中的变量赋值
# $tplVal 模板中的变量名
function assign($tplVal ,$value="")
{
if (is_array($tplVal))
{
foreach ($tplVal as $key => $val)
{
if (!empty($key))
$this->var[$key] = $val ;
}
}
else {
if (!empty($tplVal))
$this->var[$tplVal] = $value ;
}
}
#输出文件内容函数
function display($tplFile,$tplId=0,$cacheId = 1,$cacheLeftTime="")
{
if (empty($tplFile)) die("Template "{$tplFile}" not exist !");
$this->cacheId = $cacheId ? md5($cacheId) : md5($this->cacheId);
$cacheFile = $this->path. "/".$this->cacheDir."/".$tplFile.$this->cacheId ;
if ($this->check_cache($cacheFile,$cacheLeftTime)) #当缓存文件存在且不过期时直接从缓存文件读取内容
{
echo $this->read_file($cacheFile);
}else {
$tpl = $this->path."/".$this->tplDir."/".$tplFile.".".$this->tplExt;
$tplContent = $this->read_file($tpl); #读取模板文件的内容
$compileContent= $this->compile_file($tplContent); #对读取出来的文件进行编译
$this->tplId = $tplId ? $tplId : $this->tplId ;
$compileFile = $this->path."/".$this->compileDir."/".md5($this->tplId)."".$tplFile.".php";
$this->write_file($compileFile,$compileContent);#将编译后的内容写入相应的文件中;
@extract($this->var);
ob_start();
include_once($compileFile);
$content = ob_get_contents() ;
ob_end_clean() ;
if ($this->isCache){
$this->write_file($cacheFile,$content) ;# 帮编译好的内容写入缓存文件
}
echo $content ;
}
}
/* function trim_tag($content)
{
$content = str_replace($this->startTag,"",$content);
$content = str_replace($this->endTag,"",$content);
//$content = trim($content);
return $content ;
}*/
# 编译文件函数
function compile_file($content=null)
{
$content = $content ? $content :die("Compile fail!") ;
//$content = $this->trim_tag($content);
$content = preg_replace($this->pattern,$this->replacement,$content);
return $content;
}
#解析包含文件
function inc_file($filename,$tplId="",$cacheId="",$cacheLeftTime="")
{
$file = $this->path."/".$this->tplDir."/".$filename ;
if (file_exists($file))
{
$filename = str_replace(".".$this->tplExt,"",$filename);
return $this->display($filename,$tplId,$cacheId,$cacheLeftTime);
}
else die("Template "{$filename}" not exist");
}
#读取文件内容函数
function read_file($filename)
{
if (!file_exists($filename)) die("Read file fail") ;
return file_get_contents($filename);
}
#内容写入函数
function write_file($filename,$content,$mode="wb")
{
$filename = trim($filename);
$content = $content ? stripslashes(trim($content)) : exit();
if (!file_exists($filename))
{
$array = explode("/",$filename);
$count = count($array);
$path = "";
for ($i = 0 ; $i <$count-1 ; ++$i )
{
if(!file_exists($path .= $array[$i]."/"))
{
mkdir($path,0777);
}
}
}
$handle = fopen($filename,$mode) ;
fwrite($handle,$content);
fclose($handle);
return true;
}
# 清除缓存
function clear_dir($dir="")
{
$dir = $this->path."/".$dir;
$handle = opendir($dir);
if (file_exists($dir))
{
while ($file = readdir($handle))
{
if ($file !="." && $file != "..")
unlink($dir."/".$file);
}
closedir($handle);
return true;
}
else {
return false;
}
}
#清除所有缓存
function clear_all_cache()
{
if ($this->clear_dir($this->cacheDir) && $this->clear_dir($this->compileDir))
return true;
}
#检查缓存是否过期
function check_cache($cacheFile,$cacheLeftTime="")
{
$cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime;
if (!file_exists($cacheFile)) return false ;
$time = $this->get_time($cacheFile) + $cacheLeftTime ;
if ($time <time())
{
unlink($cacheFile);
return false;
}
return true;
}
# 获取文件最后编辑时间
function get_time($filename)
{
if (!file_exists($filename)) return false;
return filemtime($filename);
}
}
php 读取 sogourank 与 ChinaRank信息
function sogouRank($domain)
{
$rank = '';
$pr = 0;
$content = get_content('http://www.sogou.com/web?query='.$domain);
if(preg_match("/</span>([0-9]{1,})</dd>/", $content, $matches))
{
$pr = intval($matches[1]);
$width = ceil(65*$pr/100);
$rank = '<img src="./images/sg_left.gif" width="2" height="11" /><img src="./images/sg_left_img.gif" width="'.$width.'" height="11" /><img src="./images/sg_right_img.gif" width="'.(65-$width).'" height="11" /><img src="./images/sg_right.gif" width="2" height="11" />';
}
$rank = '<a href="http://www.sogou.com/web?query=link%3A'.$domain.'" target="_blank" title="搜狗Rank:'.$pr.'">'.$rank.'</a> '.$pr;
return $rank;
}
function ChinaRank($domain)
{
$rank = '';
$content = get_content('http://www.chinarank.org.cn/detail/Info.do?url='.$domain);
if(preg_match("/<strong>排名</strong>(.*)</tr>/", $content, $matches))
{
$p = trim(str_replace('</td>', '', $matches[1]));
$p = explode("<td>", $p);
if(isset($p[1])) $rank.= ' 今日:'.$p[1];
if(isset($p[2])) $rank.= ' 本周:'.$p[2];
if(isset($p[3])) $rank.= ' 三月:'.$p[3];
}
$rank = '<a href="http://www.chinarank.org.cn/detail/Info.do?url='.$domain.'" target="_blank">'.$rank.'</a>';
return $rank;
}
function Alexa($domain)
{
$alexa = '';
$content = get_content('http://www.alexa.com/data/details/traffic_details?url='.$domain);
if(preg_match("/3 mos. Change([sS]*?)</table>/", $content, $matches))
{
$change = strpos($matches[1], 'down_arrow.gif') ? '下降' : '上升';
$p = strip_tags($matches[1], '<td>');
$p = trim(str_replace(array(" ", "n", "</td>"), array('', '', ''), $p));
$p = explode("<td>", $p);
if(isset($p[1])) $alexa.= ' 今日:'.$p[1];
if(isset($p[2])) $alexa.= ' 本周:'.$p[2];
if(isset($p[3])) $alexa.= ' 本月:'.$p[3];
if(isset($p[4])) $alexa.= ' '.$change.':'.$p[4];
}
if(preg_match("/Review for $domain:</span> (.*)<br>/", $content, $matches))
{
$alexa = $alexa.' 等级:'.$matches[1];
}
$alexa = '<a href="http://www.alexa.com/data/details/traffic_details?url='.$domain.'" target="_blank">'.$alexa.'</a>';
return $alexa;
}
function get_content($url)
{
if(!strpos($url, '://')) return 'Invalid URI';
$content = '';
if(ini_get('allow_url_fopen'))
{
$content = file_get_contents($url);
}
elseif(function_exists('curl_init'))
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0);
$content = curl_exec($handle);
curl_close($handle);
}
elseif(function_exists('fsockopen'))
{
$urlinfo = parse_url($url);
$host = $urlinfo['host'];
$str = explode($host, $url);
$uri = $str[1];
unset($urlinfo, $str);
$content = '';
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if(!$fp)
{
$content = 'Can Not Open Socket...';
}
else
{
$out = "GET $uri HTTP/1.1rn";
$out.= "Host: $host rn";
$out.= "Accept: */*rn";
$out.= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
$out.= "Connection: Closernrn";
fputs($fp, $out);
while (!feof($fp))
{
$content .= fgets($fp, 4069);
}
fclose($fp);
}
}
if(empty($content)) $content = 'Can Not Open Url, Please Check You Server ... <br/>For More Information, Please Visit www.111cn.net;
return $content;
}
相关文章
php 中file_get_contents超时问题的解决方法
file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25- 有时为了网站安全和版权问题,会对自己写的php源码进行加密,在php加密技术上最常用的是zend公司的zend guard 加密软件,现在我们来图文讲解一下。 下面就简单说说如何...2016-11-25
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
- 下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
- ps软件是现在很多人都会使用到的,HSL面板在ps软件中又有着非常独特的作用。这次文章就给大家介绍下ps怎么使用HSL面板,还不知道使用方法的下面一起来看看。  ...2017-07-06
- ps软件是现在非常受大家喜欢的一款软件,有着非常不错的使用功能。这次文章就给大家介绍下ps把文字背景变透明的操作方法,喜欢的一起来看看。 1、使用Photoshop软件...2017-07-06
intellij idea快速查看当前类中的所有方法(推荐)
这篇文章主要介绍了intellij idea快速查看当前类中的所有方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-09-02- 1.在没有设置默认值的情况下: 复制代码 代码如下:SELECT userinfo.id, user_name, role, adm_regionid, region_name , create_timeFROM userinfoLEFT JOIN region ON userinfo.adm_regionid = region.id 结果:...2014-05-31
js导出table数据到excel即导出为EXCEL文档的方法
复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta ht...2013-10-13PHP Fatal error: Cannot use object of type stdClass as array in错误
下面一起来看看在php开发中碰到PHP Fatal error: Cannot use object of type stdClass as array in错误问题的解决办法吧。 普通的数组出现如下错误 代码...2016-11-25- 批量更新mysql更新语句很简单,更新一条数据的某个字段,一般这样写:复制代码 代码如下:UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_value';如果更新同一字段为同一个值,mysql也很简单,修改下where即...2013-10-04
- ps软件是一款非常不错的图片处理软件,有着非常不错的使用效果。这次文章要给大家介绍的是ps怎么制作倒影,一起来看看设计倒影的方法。 用ps怎么做倒影最终效果̳...2017-07-06
- 许多的朋友对于Plesk控制面板应用不是非常的了解特别是英文版的Plesk控制面板,在这里小编整理了一些关于Plesk控制面板常用的使用方案整理,具体如下。 本文基于Linu...2016-10-10
- 本文涉及的主题虽然很基础,在许多人看来属于小伎俩,但在JavaScript基础知识中属于一个综合性的话题。这里会涉及到对象属性的封装、原型、构造函数、闭包以及立即执行表达式等知识。公有方法 公有方法就是能被外部访问...2015-11-08
安卓手机wifi打不开修复教程,安卓手机wifi打不开解决方法
手机wifi打不开?让小编来告诉你如何解决。还不知道的朋友快来看看。 手机wifi是现在生活中最常用的手机功能,但是遇到手机wifi打不开的情况该怎么办呢?如果手机wifi...2016-12-21- 最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
使用insertAfter()方法在现有元素后添加一个新元素
复制代码 代码如下: //在现有元素后添加一个新元素 function insertAfter(newElement, targetElement){ var parent = targetElement.parentNode; if (parent.lastChild == targetElement){ parent.appendChild(newEl...2014-05-31- 单个字符分割 string s="abcdeabcdeabcde"; string[] sArray=s.Split('c'); foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果: ab de...2020-06-25
jQuery 1.9使用$.support替代$.browser的使用方法
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31