/*獲得客戶端ip地址*/
function getIP() {
if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")) {
$ip = getenv("HTTP_CLIENT_IP");
}
else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"),"unknown")) {
$ip = getenv("REMOTE_ADDR");
}
else if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],"unknown")) {
$ip = $_SERVER['REMOTE_ADDR'];
}
else {
$ip = "unknown";
}
return($ip);
}
/*驗證IP地址函數*/
function checkIP($ip) {
return preg_match((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?);
}
/*用戶輸入內容過濾函數*/
function getStr($str) {
$tmpstr = trim($str);
$tmpstr = strip_tags($tmpstr);
$tmpstr = htmlspecialchars($tmpstr);
/*加入字符轉義*/
$tmpstr = addslashes($tmpstr);
return $tmpstr;
}
/*容量大小計算函數*/
function sizecount($filesize) {
if($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' G';
} elseif($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' M';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' K';
} else {
$filesize = $filesize . ' bytes';
}
return $filesize;
}
/*簡單防SQL注入函數*/
function getSQL($feild) {
$tmpfeild = mysql_escape_string($feild);
return $tmpfeild;
}
/*$num必須為英文字符或數字0-9*/
function getNums($num) {
return (ctype_alnum($num));
}
/*$char必須為英文字符*/
function getChar($char) {
return (ctype_alpha($char));
}
/*匹配qq(5-12)位*/
function getQQ($qq) {
return preg_match("/^\b[0-9]{5,12}\b/",$qq);
}
/*匹配電子郵件地址*/
function getEmail($email) {
return strlen($email)>6 && preg_match("/^\w+@(\w+\.)+[com]|[cn]$/" , $email);
// preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/",$email);
}
/*生成email連接*/
function emailconv($email,$tolink=1) {
$email=str_replace(array('@','.'),array('@','.'),$email);
return $tolink ? '<a href="mailto: '.$email.'">'.$email.'</a>':$email;
}
/*檢查ip是否被允許訪問*/
function ipaccess($ip,$accesslist) {
return preg_match("/^(".str_replace(array("\r\n",' '),array('|',''),preg_quote($accesslist,'/')).")/",$ip);
}
/*若標題過長,此函數可顯示前幾個字符,剩余字符用...代替*/
function cutstr($string, $length) {
if(strlen($string) > $length) {
for($i = 0; $i < $length - 3; $i++) {
/*返回字符的序數值*/
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
return $strcut.' ...';
} else {
return $string;
}
}