最近写了一个项目,在做安装目录时,写了几个安装目录常用的函数

最近写了一个项目,在做安装目录时,写了几个安装目录常用的函数,平时一般都是为企业开发,一般很少去写安装目录。只需要去帮他们配置上服务器即可,而这次是又遇到一个站长,他们不懂代码,就要求自动安装。

服务端信息

//获取服务器信息
function get_webinfo() {
    $result = array();
    $result['self'] = strtolower($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
    $result['domain'] = strtolower($_SERVER['SERVER_NAME']);
    $result['agent'] = $_SERVER['HTTP_USER_AGENT'];
    $result['referer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
    $result['scheme'] = $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $result['reuri'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    $result['port'] = $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
    $result['url'] = $result['scheme'] . $result['domain'] . $result['port'];
    return $result;
}

域名信息

//获取主域名
function get_current_domain() {
    $ServerName = strtolower($_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);
    if($i=strpos($ServerName,':')) $ServerName = substr($ServerName,0,$i);
    return str_replace(array('http://','https://'),'',$ServerName);
}
//获取顶级域名
function get_fl_domain($full_domain='') {
    static $library = '';
    if(!$library) {
        $library = file_get_contents(MUDDER_DATA . 'domain_library.inc');
        if($library) $library = str_replace('.', '\.', preg_replace("/\s*(\r\n|\n\r|\n|\r)\s*/", "|", $library));
        if(!$library) $library = 'com\.cn|com\.hk|net|com|cn|us|tw|hk';
    }
    $url = $full_domain ? $full_domain : get_current_domain();
    if(preg_match('/[\w][\w-]*\.(?:' . $library . ')(\/|$)/isU', $url, $domain)) {
        return rtrim($domain[0], '/');
    }
    return;
}
//获取二级域名前缀
function get_sl_domain() {
    $domain = get_current_domain();
 $list = explode('.', $domain);
 if(count($list)<=2 || $list[0]=='www') return '';
 return $list[0];
}



前台输入安全信息

//替换全角数字
function cdc2dbc($number) {
    $search_arr = array('0','1','2','3','4','5','6','7','8','9');
    $replace_arr = array('0','1','2','3','4','5','6','7','8','9');
    return str_replace($search_arr, $replace_arr, $number);
}
//获取随机数 ALL(数字或字母),NUM(数字),WORD(字母)
function random($length=8, $idtype='ALL') {
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    $hash = '';
    for ($i = 0; $i < $length;  $i++) {
        if ( 'NUM' == $idtype ) {
            if (0==$i) {
                $hash .= chr(rand(49, 57));
            } else {
                $hash .= chr(rand(48, 57));
            }
        } else if ( 'WORD' == $idtype ){
            $hash .= chr(rand(65, 90));
        } else {
            if ( 0==$i ) {
                $hash .= chr(rand(65, 90));
            } else {
                $hash .= (0==rand(0,1))?chr(rand(65, 90)):chr(rand(48,57));
            }
        }
    }
    return $hash;
}
//过滤SQL
function strip_sql($string) {
    $pattern_arr = array("/ union /i", "/ select /i", "/ update /i", "/ outfile /i", "/ or /i");
    $replace_arr = array('&nbsp;union&nbsp;', '&nbsp;select&nbsp;', '&nbsp;update&nbsp;','&nbsp;outfile&nbsp;', '&nbsp;or&nbsp;');
    return is_array($string) ? array_map('strip_sql', $string) : preg_replace($pattern_arr, $replace_arr, $string);
}
//过滤orderby
function strip_order($string) {
    $string = preg_replace('/.?select.+from.+/i', '', $string);
    $string = preg_replace("/.?delete.+from.+/i", '', $string);
    $string = preg_replace("/.?update.+set.+/i", '', $string);
    $string = preg_replace("/.?select.+union.+/i", '', $string);
    return $string;
}


RUL定向,确保任何环境下安装前台CSS、JS等路径文件不出差错

// URL重定向
function redirect($url, $time=0, $msg='') {
    //多行URL地址支持
    $url = str_replace(array("\n", "\r"), '', $url);
    if (empty($msg))
        $msg = "系统将在{$time}秒之后自动跳转到{$url}!";
    if (!headers_sent()) {
        // redirect
        if (0 === $time) {
            header('Location: ' . $url);
        } else {
            header("refresh:{$time};url={$url}");
            echo($msg);
        }
        exit();
    } else {
        $str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
        if ($time != 0)
            $str .= $msg;
        exit($str);
    }
}


输出安全的HTML


//输出安全的html
function h($text, $tags = null) {
 $text = trim($text);
 //完全过滤注释
 $text = preg_replace('/<!--?.*-->/','',$text);
 //完全过滤动态代码
 $text = preg_replace('/<\?|\?'.'>/','',$text);
 //完全过滤js
 $text = preg_replace('/<script?.*\/script>/','',$text);
 $text = str_replace('[','&#091;',$text);
 $text = str_replace(']','&#093;',$text);
 $text = str_replace('|','&#124;',$text);
 //过滤换行符
 $text = preg_replace('/\r?\n/','',$text);
 //br
 $text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
 $text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
 //过滤危险的属性,如:过滤on事件lang js
 while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
  $text=str_replace($mat[0],$mat[1],$text);
 }
 while(preg_match('/(<[^><]+)(window\.||js:|about:|file:|document\.|vbs:|cookie)([^><]* )/i',$text,$mat)){
  $text=str_replace($mat[0],$mat[1].$mat[3],$text);
 }
 if(empty($tags)) {
  $tags = 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a';
 }
 //允许的HTML标签
 $text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);
 //过滤多余html
 $text = preg_replace('/<\/?(html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml)[^><]*>/i','',$text);
 //过滤合法的html标签
 while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
  $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
 }
 //转换引号
 while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
  $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
 }
 //过滤错误的单个引号
 while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
  $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
 }
 //转换其它所有不合法的 < >
 $text = str_replace('<','&lt;',$text);
 $text = str_replace('>','&gt;',$text);
 $text = str_replace('"','&quot;',$text);
  //反转换
 $text = str_replace('[','<',$text);
 $text = str_replace(']','>',$text);
 $text = str_replace('|','"',$text);
 //过滤多余空格
 $text = str_replace('  ',' ',$text);
 return $text;
}


你可能感兴趣的:(function,服务器,服务端,项目,result)