代码学习
时间:2007-07-06 来源:windlike
// 常位于函数文件的开头,禁止在url中直接访问该文件
if (eregi("function.php",$_SERVER['PHP_SELF'])) {
Header("Location: ../index.php");
die();
}
//返回安全字符串
function getSafeValue($value)
{
if (! get_magic_quotes_gpc()) {
return strip_tags(addslashes($value));
} else {
return strip_tags($value);
}
}
/*
get_magic_quotes_gpc 函数说明
取得 PHP 环境变量 magic_quotes_gpc 的值。
本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的溢出字符。
参考资料: http://www.cycoo.net/study/php/function.php-get_magic_quotes_gpc.htm
*/
// 将prefix分成2部分,最后一位数字代表高度使用的CSS样式,其余的部分代表正则Pattern
// 常用于关键字高亮显示
function highlight(& $item, $key, $prefix) {
$prefix1 = preg_quote(substr($prefix, 0, strlen($prefix) -1), '/');
$prefix2 = intval(substr($prefix, -1));
$item = preg_replace("/$prefix1/is", "\$0", $item);
}
// 用于URL改写,把url中的问号'?'和与符号'&'改写成斜杆'/'、'+'、','、'-'等字符
function parse_url_path()
{
/* 此方法仅用于apache服务器 */
if(isset($_SERVER[PATH_INFO])) {//若PATH_INFO可用,注意apache2默认不可用
$query_string = $_SERVER[PATH_INFO];
} else {
Return '';
}
//strip '.html','.htm'
$query_string = str_replace('.html', '', $query_string);
$query_string = str_replace('.htm', '', $query_string);
if(empty($query_string)) {
Return '';
} else {
$args = split("/", substr($query_string,1)); //分裂成数组
Return $args;
}
}
//专用函数,只用于将brand按字母排序
function GroupLetter($products,$group,$join, $categories_name = '') {
$out = array();
if(! $products) {
Return array();
}
foreach($products as $v) {
$key = $v[$group];
$value = $v[$join];
$out[$key][$join][] = "$value";
$out[$key]['models_id'][] = $v['models_id'];
}
$tmp_category_path = getCategoryPath($categories_name);
$filename = $tmp_category_path['modelsFile'];
$result = array();
foreach($out as $k=>$v) {
//定义brand的链接
$numBrand = count($v[$join]);
for ($i=0; $i" . $v[$join][$i] . "";
}
$result[] = array($group => $k, $join => $href[$k]);
}
return $result;
}
// 获得字符串的Ascii值
function AsciiCode($name) {
if(empty($name)) {
Return '';
}
$name = strval($name);
$convert_name = '';
$length = strlen($name);
for($i=0; $i$v)
$result[] = array($group => $k, $join => $v);
return $result;
}
/**
* 同上,但以,分隔
*/
function GroupBrand2($products,$group,$join) {
$out = array();
if(! $products) {
Return array();
}
foreach($products as $v) {
$key = strtoupper($v[$group]);
$value = $v[$join];
if(isset($out[$key]))
$out[$key] .= ",$value";
else
$out[$key] = "$value";
}
$result = array();
foreach($out as $k=>$v)
$result[] = array($group => $k, $join => $v);
return $result;
}
/**
*此函数对指定的一个二维数组$products中$group进行筛选,将相同$group的$join值只留一个
*@Parameters:$products,$group,$join,其中$products包含了要操作的数据
*@Return:返回一个二维数组$result,数组包括两个键名:$group,$join
*/
function StripRepeated($products,$group,$join) {
$out = array();
if(!$products) {
Return array();
}
foreach($products as $v) {
$key = $v[$group];
$value = $v[$join];
if(! isset($out[$key]))
{
$out[$key] = "$value";
}
}
$result = array();
foreach($out as $k=>$v)
$result[] = array($group => $k, $join => $v);
return $result;
}
/**
* send a mail
* this function only can send a mail without attachment file
* it must be support by mimie class
* @sendtype => (smtp,mail);
*/
function SendMail($to = SUPPORTER_EMAIL, $from = 'unnamed', $subject, $message, $sendtype = 'smtp')
{
global $class_mail;
$domain = $_SERVER['REMOTE_ADDR'];
$send_date = strftime(DATE_FORMAT_LONG);
$send_time = strftime(DATE_TIME_FORMAT);
if(empty($subject) || !isset($subject)) {
$subject = "a email from : $domain at " . $send_date . " " . $send_time;
}
$mailmessage = $message . "this email is from :";
$mailmessage .= $domain . " at " . $send_date . " " . $send_time;
$text = $mailmessage;
$html = $mailmessage;
$class_mail->setHtml($html, $text);
$class_mail->setReturnPath($to);
$class_mail->setFrom($from);
$class_mail->setBcc(CC_EMAIL);
$class_mail->setSubject($subject);
$result = $class_mail->send(array($to), $sendtype);
if (! $result) {
Show_Error_Message("Mail send error", $class_mail->errors);
}
}
// Redirect page
function Redirect_Page($url = "/") {
$location="window.location.href='" .$url. "';";
echo $location;
return;
}
// Alert Window Message
function Alert_Message($message = "") {
echo " alert('" .$message. "');";
return;
}
//Show user define information in window
function Show_Message($title, $message)
{
$title = htmlspecialchars($title);
$message = htmlspecialchars($message);
$messageTable = "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= $title;
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= $message;
$messageTable .= "";
$messageTable .= "";
echo($messageTable);
return;
}
/**
* Show Error Message and Send a email to the supporter of the website.
*/
function Show_Error_Message($errorTitle, $errorMessage, $errorFile = __FILE__, $errorLine = __LINE__)
{
$errorFile = basename($errorFile);
$errorTitle = htmlspecialchars($errorTitle);
$errorMessage = htmlspecialchars($errorMessage);
$errorTable = "";
$errorTable .= "";
$errorTable .= " ";
$errorTable .= $errorTitle;
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "ERROR: $errorMessage";
$errorTable .= "Error occurred on line $errorLine of file $errorFile";
$errorTable .= "An email had sent to the web supporter.";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
echo($errorTable);
$errorTable .= sprintf('%s', $_SERVER["REQUEST_URI"]);
//send mail to supporter
SendMail(WEBMASTER_EMAIL, $_SERVER['REMOTE_ADDR'], $errorTitle, $errorTable);
//die();
}
if (eregi("function.php",$_SERVER['PHP_SELF'])) {
Header("Location: ../index.php");
die();
}
//返回安全字符串
function getSafeValue($value)
{
if (! get_magic_quotes_gpc()) {
return strip_tags(addslashes($value));
} else {
return strip_tags($value);
}
}
/*
get_magic_quotes_gpc 函数说明
取得 PHP 环境变量 magic_quotes_gpc 的值。
本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的溢出字符。
参考资料: http://www.cycoo.net/study/php/function.php-get_magic_quotes_gpc.htm
*/
// 将prefix分成2部分,最后一位数字代表高度使用的CSS样式,其余的部分代表正则Pattern
// 常用于关键字高亮显示
function highlight(& $item, $key, $prefix) {
$prefix1 = preg_quote(substr($prefix, 0, strlen($prefix) -1), '/');
$prefix2 = intval(substr($prefix, -1));
$item = preg_replace("/$prefix1/is", "\$0", $item);
}
// 用于URL改写,把url中的问号'?'和与符号'&'改写成斜杆'/'、'+'、','、'-'等字符
function parse_url_path()
{
/* 此方法仅用于apache服务器 */
if(isset($_SERVER[PATH_INFO])) {//若PATH_INFO可用,注意apache2默认不可用
$query_string = $_SERVER[PATH_INFO];
} else {
Return '';
}
//strip '.html','.htm'
$query_string = str_replace('.html', '', $query_string);
$query_string = str_replace('.htm', '', $query_string);
if(empty($query_string)) {
Return '';
} else {
$args = split("/", substr($query_string,1)); //分裂成数组
Return $args;
}
}
//专用函数,只用于将brand按字母排序
function GroupLetter($products,$group,$join, $categories_name = '') {
$out = array();
if(! $products) {
Return array();
}
foreach($products as $v) {
$key = $v[$group];
$value = $v[$join];
$out[$key][$join][] = "$value";
$out[$key]['models_id'][] = $v['models_id'];
}
$tmp_category_path = getCategoryPath($categories_name);
$filename = $tmp_category_path['modelsFile'];
$result = array();
foreach($out as $k=>$v) {
//定义brand的链接
$numBrand = count($v[$join]);
for ($i=0; $i" . $v[$join][$i] . "";
}
$result[] = array($group => $k, $join => $href[$k]);
}
return $result;
}
// 获得字符串的Ascii值
function AsciiCode($name) {
if(empty($name)) {
Return '';
}
$name = strval($name);
$convert_name = '';
$length = strlen($name);
for($i=0; $i$v)
$result[] = array($group => $k, $join => $v);
return $result;
}
/**
* 同上,但以,分隔
*/
function GroupBrand2($products,$group,$join) {
$out = array();
if(! $products) {
Return array();
}
foreach($products as $v) {
$key = strtoupper($v[$group]);
$value = $v[$join];
if(isset($out[$key]))
$out[$key] .= ",$value";
else
$out[$key] = "$value";
}
$result = array();
foreach($out as $k=>$v)
$result[] = array($group => $k, $join => $v);
return $result;
}
/**
*此函数对指定的一个二维数组$products中$group进行筛选,将相同$group的$join值只留一个
*@Parameters:$products,$group,$join,其中$products包含了要操作的数据
*@Return:返回一个二维数组$result,数组包括两个键名:$group,$join
*/
function StripRepeated($products,$group,$join) {
$out = array();
if(!$products) {
Return array();
}
foreach($products as $v) {
$key = $v[$group];
$value = $v[$join];
if(! isset($out[$key]))
{
$out[$key] = "$value";
}
}
$result = array();
foreach($out as $k=>$v)
$result[] = array($group => $k, $join => $v);
return $result;
}
/**
* send a mail
* this function only can send a mail without attachment file
* it must be support by mimie class
* @sendtype => (smtp,mail);
*/
function SendMail($to = SUPPORTER_EMAIL, $from = 'unnamed', $subject, $message, $sendtype = 'smtp')
{
global $class_mail;
$domain = $_SERVER['REMOTE_ADDR'];
$send_date = strftime(DATE_FORMAT_LONG);
$send_time = strftime(DATE_TIME_FORMAT);
if(empty($subject) || !isset($subject)) {
$subject = "a email from : $domain at " . $send_date . " " . $send_time;
}
$mailmessage = $message . "this email is from :";
$mailmessage .= $domain . " at " . $send_date . " " . $send_time;
$text = $mailmessage;
$html = $mailmessage;
$class_mail->setHtml($html, $text);
$class_mail->setReturnPath($to);
$class_mail->setFrom($from);
$class_mail->setBcc(CC_EMAIL);
$class_mail->setSubject($subject);
$result = $class_mail->send(array($to), $sendtype);
if (! $result) {
Show_Error_Message("Mail send error", $class_mail->errors);
}
}
// Redirect page
function Redirect_Page($url = "/") {
$location="window.location.href='" .$url. "';";
echo $location;
return;
}
// Alert Window Message
function Alert_Message($message = "") {
echo " alert('" .$message. "');";
return;
}
//Show user define information in window
function Show_Message($title, $message)
{
$title = htmlspecialchars($title);
$message = htmlspecialchars($message);
$messageTable = "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= $title;
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= "";
$messageTable .= $message;
$messageTable .= "";
$messageTable .= "";
echo($messageTable);
return;
}
/**
* Show Error Message and Send a email to the supporter of the website.
*/
function Show_Error_Message($errorTitle, $errorMessage, $errorFile = __FILE__, $errorLine = __LINE__)
{
$errorFile = basename($errorFile);
$errorTitle = htmlspecialchars($errorTitle);
$errorMessage = htmlspecialchars($errorMessage);
$errorTable = "";
$errorTable .= "";
$errorTable .= " ";
$errorTable .= $errorTitle;
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "ERROR: $errorMessage";
$errorTable .= "Error occurred on line $errorLine of file $errorFile";
$errorTable .= "An email had sent to the web supporter.";
$errorTable .= "";
$errorTable .= "";
$errorTable .= "";
echo($errorTable);
$errorTable .= sprintf('%s', $_SERVER["REQUEST_URI"]);
//send mail to supporter
SendMail(WEBMASTER_EMAIL, $_SERVER['REMOTE_ADDR'], $errorTitle, $errorTable);
//die();
}
相关阅读 更多 +
排行榜 更多 +