專為新手寫的結合smarty的類第2/3頁
更新時間:2006年12月02日 00:00:00 作者:
復制代碼 代碼如下:
// 發(fā)送SQL語句函數(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ù)庫類型錯誤,該類目前只支持MYSQL與ACCESS兩種數(shù)據(jù)庫', 'die' );
break;
}
}
// 發(fā)送SQL語句到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語句:{{$sql}}執(zhí)行失敗,原因是:{{$mysql_err}}", 'die' );
}
return $rs;
}
// 發(fā)送SQL語句到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語句:{{$sql}}執(zhí)行失敗,原因是:{{$odbc_err}}", 'die' );
}
return $rs;
}
// 獲取查詢返回函數(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ù)庫類型錯誤,該類目前只支持MYSQL與ACCESS兩種數(shù)據(jù)庫', 'die');
break;
}
}
// 獲取MYSQL查詢返回函數(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查詢返回函數(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)錯誤函數(shù)
public function sys_err( $err_msg, $method, $err_notice = '很抱歉,本站發(fā)生系統(tǒng)錯誤,請稍候再試。' )
{
$this->err_record( 'sys', $err_msg );
switch ( $method )
{
case 'keep':
return;
break;
default:
$this->err_notice( $err_notice );
break;
}
}
// 獲取用戶錯誤函數(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;
}
}
// 記錄錯誤函數(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' );
}
// 文件寫入函數(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 );
}
// 提示錯誤函數(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( '錯誤提示頁面丟失', 'keep' );
die( '很抱歉,本站發(fā)生系統(tǒ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ù)
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)頁面函數(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();
}
// 轉到靜態(tài)頁面
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;
}
}
}
}
?>
復制代碼 代碼如下:
<?php
// 添斜線
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;
}
?>
復制代碼 代碼如下:
<?php
function m_check_fill( $check_arr = array() )
{
$pattern['idname'] = array('!^[a-z0-9]{3,8}$!i' , '您輸入的用戶名格式不正確');
$pattern['username'] = array('!^.{4,12}$!' , '您輸入的用戶昵稱格式不正確');
$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}$!' , '您輸入的真實姓名格式不正確');
$pattern['id_card'] = array('!^[0-9]{15}([0-9]{2}[a-z0-9])?$!i' , '您輸入的身份證號碼格式不正確');
$pattern['title'] = array('!^.{1,255}$!' , '您輸入的帖子標題格式不正確');
$pattern['block_name'] = array('!^.{3,12}$!' , '您輸入的板塊名稱格式不正確');
$err_msg = '';
if ( !is_array( $check_arr ) )
{
return '很抱歉,系統(tǒng)出現(xiàn)參數(shù)傳遞錯誤,請通知管理員,謝謝合作';
}
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;
}
?>
復制代碼 代碼如下:
<?php
//防止跨站攻擊函數(shù)
function m_check_key( $no_reload = true )
{
if ( empty( $_COOKIE['check'] ) || $_COOKIE['check'] != $_POST['check'] )
{
setcookie( 'check', '', time()-86400 );
return '發(fā)生錯誤,這也許是由于您重復提交數(shù)據(jù)造成的,請重新提交。<br>';
}
if ( $no_reload == true )
{
setcookie( 'check', '', time()-86400 );
}
}
?>
復制代碼 代碼如下:
<?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;
}
?>
復制代碼 代碼如下:
<?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 . '…';
}
?>
相關文章
使用PHP socke 向指定頁面提交數(shù)據(jù)
一直以為有了ajax別人網(wǎng)站的數(shù)據(jù)就可以拿過來用,這也是我這幾天想的一個方像,但是用了firefox測試之后,現(xiàn)在不能,2008-07-07PHP中使用xmlreader讀取xml數(shù)據(jù)示例
這篇文章主要介紹了PHP中使用xmlreader讀取xml數(shù)據(jù)示例,本文示例相對簡單,只包含了一個讀取功能,需要的朋友可以參考下2014-12-12php自定義函數(shù)call_user_func和call_user_func_array詳解
看UCenter的時候有一個函數(shù)call_user_func,百思不得其解,因為我以為是自己定義的函數(shù),結果到處都找不到,后來百度了一下才知道call_user_func是內置函數(shù)2011-07-07PHP通過get方法獲得form表單數(shù)據(jù)方法總結
這篇文章我們給大家介紹了PHP如何通過get的方式來得到獲取form表單數(shù)據(jù)的方法,有需要的朋友們參考下。2018-09-09