/** * 网站的网站地址 */ function server_host($remote=false) { $host = $_SERVER['HTTP_HOST']; if($remote) return base_url(true); else return 'http://'.$host.base_url(); } /** * 页面跳转 */ function redirect($uri = '', $method = 'location', $status_code = null) { $base_url = App::get_app_info('base_url'); switch($method) { case 'refresh' : if (has_base_url($uri)) { header("Refresh:0;url=".$uri); } else { header("Refresh:0;url=".$base_url.$uri); } break; default : $hasbaseurl = has_base_url($uri); $thisuri = $_SERVER['REQUEST_URI']; @header("Last-Modified: " .gmdate ("D, d M Y H:i:s", time()). " GMT"); @header("Expires: " .gmdate ("D, d M Y H:i:s", time() -3600). " GMT"); @header("Cache-Control: private"); if (isset($status_code)) { switch ($status_code) { case 301: header("HTTP/1.1 301 Moved Permanently");break; case 302: header("HTTP/1.1 302 Found");break; case 303: header("HTTP/1.1 303 See Other");break; case 304: header("HTTP/1.1 304 Not Modified");exit; case 307: header("HTTP/1.1 307 Temporary Redirect");break; } } if ($hasbaseurl == 0) { header("location:".$base_url.$uri); } else { header("location:".$uri); } break; } exit; } /** * 变量输出 +---------------------------------------------------------- * @param string $var 变量名 * @param string $label 显示标签 * @param string $echo 是否显示 * @return string * */ function dump($var, $echo=true,$label=null, $strict=true) { $label = ($label===null) ? '' : rtrim($label) . ' '; if(!$strict) { if (ini_get('html_errors')) { $output = print_r($var, true); $output = "<pre>".$label.htmlspecialchars($output,ENT_QUOTES,'utf-8')."</pre>"; } else { $output = $label . " : " . print_r($var, true); } }else { ob_start(); var_dump($var); $output = ob_get_clean(); if(!extension_loaded('xdebug')) { $output = preg_replace("//]/=/>/n(/s+)/m", "] => ", $output); $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES,'utf-8') . '</pre>'; } } if ($echo) { echo($output); return null; }else { return $output; } } /** * 模拟http请求 */ function http_request($url='',$type='get',$data=array(),$session=false) { if(empty($url) or !in_array($type,array('post','get'))) return array('success'=>false,'data'=>''); if(!function_exists('curl_init')) return array('success'=>false,'data'=>'curl not install'); //初始化curl $ch = curl_init(); //设置url curl_setopt($ch, CURLOPT_URL, $url); //有返回值,而不是执行成功否 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //连接最多5秒 curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:")); //如果需要验证session设置 if($session) { $sess = new Session(); $cookie = $sess->sess_cookie."=". urlencode(stripslashes($_COOKIE[$sess->sess_cookie])); curl_setopt($ch, CURLOPT_COOKIE , $cookie); } //如果是post方式设置 if($type == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } //执行请求,完成后关闭 $result = curl_exec($ch); curl_close($ch); if(empty($result)) { return array('success'=>false,'data'=>'url request failed'); } else { $data = json_decode(trim($result),true); if(is_array($data)) return $data; else return array('success'=>true,'data'=>$result); } } /** * 区间调试开始 * @param string $label 标记名称 * @return void */ function debug_start($label='') { $GLOBALS[$label]['_beginTime'] = microtime(TRUE); if ( function_exists('memory_get_usage') ) $GLOBALS[$label]['memoryUseStartTime'] = memory_get_usage(); } //--------------------------------------------------------------- /** * 区间调试结束,显示指定标记到当前位置的调试 * @param string $label 标记名称 * @return void */ function debug_end($label='',$output=true) { if(!isset($GLOBALS[$label]['_beginTime'])) return ; $GLOBALS[$label]['_endTime'] = microtime(TRUE); $str = '<div style="text-align:center;width:100%">Process '.$label. ': Times '.number_format($GLOBALS[$label]['_endTime']-$GLOBALS[$label]['_beginTime'],6).'s '; if ( function_exists('memory_get_usage') ) { $GLOBALS[$label]['memoryUseEndTime'] = memory_get_usage(); $str .= ' Memories '.number_format(($GLOBALS[$label]['memoryUseEndTime']- $GLOBALS[$label]['memoryUseStartTime'])/1024).' k'; } $str .= '</div>'; if($output) echo $str; else return $str; }