專(zhuān)為新手寫(xiě)的結(jié)合smarty的類(lèi)第2/3頁(yè)
更新時(shí)間:2006年12月02日 00:00:00 作者:
復(fù)制代碼 代碼如下:
// 發(fā)送SQL語(yǔ)句函數(shù)
public function send_query( $sql )
{
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
return $this->send_mysql_query( $sql );
break;
case 'ACCESS' :
return $this->send_odbc_query( $sql );
break;
default :
$this->sys_err( '數(shù)據(jù)庫(kù)類(lèi)型錯(cuò)誤,該類(lèi)目前只支持MYSQL與ACCESS兩種數(shù)據(jù)庫(kù)', 'die' );
break;
}
}
// 發(fā)送SQL語(yǔ)句到MYSQL函數(shù)
private function send_mysql_query( $sql )
{
@$rs = mysql_query( $sql, $this->conn );
if ( $rs == false )
{
$mysql_err = mysql_error();
$this->sys_err( "SQL語(yǔ)句:{{$sql}}執(zhí)行失敗,原因是:{{$mysql_err}}", 'die' );
}
return $rs;
}
// 發(fā)送SQL語(yǔ)句到ACCESS函數(shù)
private function send_odbc_query( $sql )
{
@$rs = odbc_exec( $this->conn, $sql );
if ( $rs == false )
{
$odbc_err = odbc_errormsg( $this->conn );
$this->sys_err( "SQL語(yǔ)句:{{$sql}}執(zhí)行失敗,原因是:{{$odbc_err}}", 'die' );
}
return $rs;
}
// 獲取查詢(xún)返回函數(shù)
public function select_query( $sql, $retuen_res = false )
{
$res = $this->send_query( $sql );
if ( $retuen_res == true )
{
return $res;
}
switch ( strtoupper( $this->config->database ) )
{
case 'MYSQL' :
return $this->select_mysql_query( $res );
break;
case 'ACCESS' :
return $this->select_access_query( $res );
break;
default :
$this->sys_err( '數(shù)據(jù)庫(kù)類(lèi)型錯(cuò)誤,該類(lèi)目前只支持MYSQL與ACCESS兩種數(shù)據(jù)庫(kù)', 'die');
break;
}
}
// 獲取MYSQL查詢(xún)返回函數(shù)
private function select_mysql_query( $res )
{
$arr = array();
while ( false != ( $rs = mysql_fetch_assoc( $res ) ) )
{
$arr[] = $rs;
}
mysql_free_result( $res );
return ( count( $arr ) > 0 ? $arr : false );
}
// 獲取ACCCESS查詢(xún)返回函數(shù)
private function select_access_query( $res )
{
$arr = array();
while ( false != ( $rs = odbc_fetch_array( $res ) ) )
{
$arr[] = $rs;
}
odbc_free_result( $res );
return ( count( $arr ) > 0 ? $arr : false );
}
// 獲取系統(tǒng)錯(cuò)誤函數(shù)
public function sys_err( $err_msg, $method, $err_notice = '很抱歉,本站發(fā)生系統(tǒng)錯(cuò)誤,請(qǐng)稍候再試。' )
{
$this->err_record( 'sys', $err_msg );
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice );
break;
}
}
// 獲取用戶(hù)錯(cuò)誤函數(shù)
public function user_err( $err_notice, $method, $re_href = '', $err_msg = '' )
{
if ( !empty( $err_msg ) )
{
$this->err_record( 'user', $err_msg );
}
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice, $re_href );
break;
}
}
// 記錄錯(cuò)誤函數(shù)
private function err_record( $type, $err_msg )
{
$err_url = $this->the_dir . 'lib/error/';
$err_url .= ( $type == 'sys' ? 'system.err' : 'user.err' );
$record_msg = date( 'Y-m-d H:i:s' ) . '|' . $err_msg . "\n";
$this->file_put( $err_url, $record_msg, 'ab' );
}
// 文件寫(xiě)入函數(shù)
public function file_put( $url, $content, $method )
{
$dir = str_replace( basename( $url ), '', $url );
if ( !file_exists( $dir ) )
{
$this->sys_err( "{{$dir}}文件夾不存在", 'die' );
}
@$f = fopen( $url, $method );
@flock( $f, LOCK_NM );
@fwrite( $f, $content, strlen( $content ) );
@flock( $f, LOCK_UN );
@fclose( $f );
}
// 提示錯(cuò)誤函數(shù)
protected function err_notice( $err_notice, $re_href = '' )
{
$err_page = $this->the_dir . $this->err_page;
if ( !file_exists( $err_page ) )
{
$this->sys_err( '錯(cuò)誤提示頁(yè)面丟失', 'keep' );
die( '很抱歉,本站發(fā)生系統(tǒng)錯(cuò)誤,請(qǐng)稍候再試。' );
}
$err_html = file_get_contents( $err_page );
$err_html = str_replace( '<%$err_notice%>', $err_notice, $err_html);
$err_html = str_replace( '<%$this_url%>', $this->the_dir, $err_html);
$err_html = str_replace( '<%$re_href%>', $re_href, $err_html);
echo $err_html;
exit;
}
// 用于設(shè)定模版文件路徑的函數(shù)
function set_dir( $file_dir = 'smarty_file' )
{
if ( !preg_match( '{/$}', $file_dir ) )
{
$file_dir .= '/';
}
if ( !file_exists( $this->the_dir . $file_dir . $this->template_dir ) )
{
$this->sys_err( 'smarty模版路徑丟失', 'die' );
}
if ( !file_exists( $this->the_dir . $file_dir . $this->compile_dir ) )
{
$this->sys_err( 'smarty編譯路徑丟失', 'die' );
}
if ( !file_exists( $this->the_dir . $file_dir . $this->cache_dir ) )
{
$this->sys_err( 'smarty緩存路徑丟失', 'die' );
}
$this->template_dir = $this->the_dir . $file_dir . $this->template_dir;
$this->compile_dir = $this->the_dir . $file_dir . $this->compile_dir;
$this->cache_dir = $this->the_dir . $file_dir . $this->cache_dir;
}
// 生成靜態(tài)頁(yè)面函數(shù)
public function create_html( $tpl, $file_url = '', $html_name = '' )
{
$html_tpl = $this->fetch( $tpl );
//生成靜態(tài)文件的文件名
if ( empty( $html_name ) )
{
$file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );
$file_name = str_replace( '.php', ".{$this->html_cache}", $file_name );
}
else
{
$file_name = $html_name;
}
if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) )
{
$file_url .= '/';
}
$file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}";
$file_url .= $file_name;
$this->file_put( $file_url, $html_tpl, 'wb');
header("location:{$file_url}");
exit();
}
// 轉(zhuǎn)到靜態(tài)頁(yè)面
public function goto_html( $left_time = null, $file_url = '', $html_name = '' )
{
$left_time = ( $left_time == null ? $this->html_cache_lifetime : intval( $left_time ) );
//獲取靜態(tài)文件的文件名
if ( empty( $html_name ) )
{
$file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );
$file_name = str_replace( '.php', ".{$this->html_cache}", $file_name );
}
else
{
$file_name = $html_name;
}
if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) )
{
$file_url .= '/';
}
$file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}";
$file_url .= $file_name;
if ( !file_exists( $file_url) )
{
return;
}
if ( $left_time == -1 )
{
header("location:{$file_url}");
exit;
}
else
{
@$fmtime = filemtime( $file_url );
$fmtime = intval( $fmtime );
if ( time() - $fmtime <= $left_time )
{
header("location:{$file_url}");
exit;
}
}
}
}
?>
復(fù)制代碼 代碼如下:
<?php
// 添斜線(xiàn)
function m_addslashes( $gpc )
{
if ( !get_magic_quotes_gpc() )
{
if( is_array( $gpc ) )
{
while( list( $k, $v ) = each( $gpc ) )
{
if( is_array( $gpc[$k] ) )
{
while( list( $k2, $v2 ) = each( $gpc[$k] ) )
{
$gpc[$k][$k2] = addslashes( $v2 );
}
@reset( $gpc[$k] );
}
else
{
$gpc[$k] = addslashes( $v );
}
}
@reset( $gpc );
}
else
{
$gpc = addslashes( $gpc );
}
}
return $gpc;
}
?>
復(fù)制代碼 代碼如下:
<?php
function m_check_fill( $check_arr = array() )
{
$pattern['idname'] = array('!^[a-z0-9]{3,8}$!i' , '您輸入的用戶(hù)名格式不正確');
$pattern['username'] = array('!^.{4,12}$!' , '您輸入的用戶(hù)昵稱(chēng)格式不正確');
$pattern['email'] = array('!^([a-z0-9]+(\.[a-z0-9]+)?@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?)?$!i' , '您輸入的電子郵箱格式不正確');
$pattern['oicq'] = array('!^([0-9]{4,12})?$!' , '您輸入的OICQ格式不正確');
$pattern['password'] = array('!^[a-z0-9]{6,14}$!i' , '您輸入的密碼格式不正確');
$pattern['real_name'] = array('!^.{4,20}$!' , '您輸入的真實(shí)姓名格式不正確');
$pattern['id_card'] = array('!^[0-9]{15}([0-9]{2}[a-z0-9])?$!i' , '您輸入的身份證號(hào)碼格式不正確');
$pattern['title'] = array('!^.{1,255}$!' , '您輸入的帖子標(biāo)題格式不正確');
$pattern['block_name'] = array('!^.{3,12}$!' , '您輸入的板塊名稱(chēng)格式不正確');
$err_msg = '';
if ( !is_array( $check_arr ) )
{
return '很抱歉,系統(tǒng)出現(xiàn)參數(shù)傳遞錯(cuò)誤,請(qǐng)通知管理員,謝謝合作';
}
foreach ( $check_arr as $key => $value )
{
if ( !empty( $pattern[$key] ) )
{
if( !preg_match( $pattern[$key][0], $value ) )
{
$err_msg .= $pattern[$key][1] . '<br>';
}
}
}
return $err_msg;
}
?>
復(fù)制代碼 代碼如下:
<?php
//防止跨站攻擊函數(shù)
function m_check_key( $no_reload = true )
{
if ( empty( $_COOKIE['check'] ) || $_COOKIE['check'] != $_POST['check'] )
{
setcookie( 'check', '', time()-86400 );
return '發(fā)生錯(cuò)誤,這也許是由于您重復(fù)提交數(shù)據(jù)造成的,請(qǐng)重新提交。<br>';
}
if ( $no_reload == true )
{
setcookie( 'check', '', time()-86400 );
}
}
?>
復(fù)制代碼 代碼如下:
<?php
//防止跨站攻擊函數(shù)
function m_md5( $password = '' )
{
if ( empty( $password ) )
{
$password = md5( uniqid ( rand(), true ) );
setcookie( 'check', $password );
}
else
{
$key = 'machine';
$password = md5( $password . $key );
}
return $password;
}
?>
復(fù)制代碼 代碼如下:
<?php
//中文截取函數(shù)
function m_cnsubstr( $string, $sublen )
{
if( $sublen >= strlen( $string ) )
{
return $string;
}
$s = "";
for ( $i = 0; $i < $sublen; $i++ )
{
if( ord( $string{$i} ) > 127 )
{
$s .= $string{$i} . $string{++$i};
continue;
}
else
{
$s .= $string{$i};
continue;
}
}
return $s . '…';
}
?>
相關(guān)文章
基于win2003虛擬機(jī)中apache服務(wù)器的訪(fǎng)問(wèn)
下面小編就為大家?guī)?lái)一篇基于win2003虛擬機(jī)中apache服務(wù)器的訪(fǎng)問(wèn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08使用PHP socke 向指定頁(yè)面提交數(shù)據(jù)
一直以為有了ajax別人網(wǎng)站的數(shù)據(jù)就可以拿過(guò)來(lái)用,這也是我這幾天想的一個(gè)方像,但是用了firefox測(cè)試之后,現(xiàn)在不能,2008-07-07PHP中使用xmlreader讀取xml數(shù)據(jù)示例
這篇文章主要介紹了PHP中使用xmlreader讀取xml數(shù)據(jù)示例,本文示例相對(duì)簡(jiǎn)單,只包含了一個(gè)讀取功能,需要的朋友可以參考下2014-12-12PHP獲取當(dāng)前日期及本周一是幾月幾號(hào)的方法
這篇文章主要介紹了PHP獲取當(dāng)前日期及本周一是幾月幾號(hào)的方法,涉及php時(shí)間戳、日期轉(zhuǎn)換與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-03-03php自定義函數(shù)call_user_func和call_user_func_array詳解
看UCenter的時(shí)候有一個(gè)函數(shù)call_user_func,百思不得其解,因?yàn)槲乙詾槭亲约憾x的函數(shù),結(jié)果到處都找不到,后來(lái)百度了一下才知道call_user_func是內(nèi)置函數(shù)2011-07-07PHP通過(guò)get方法獲得form表單數(shù)據(jù)方法總結(jié)
這篇文章我們給大家介紹了PHP如何通過(guò)get的方式來(lái)得到獲取form表單數(shù)據(jù)的方法,有需要的朋友們參考下。2018-09-09