/*
*-------------
* 获取客户端IP
*-------------
*/
function get_ip() {
if (isset($_SERVER)) {
if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) {
$realip = $_SERVER[HTTP_X_FORWARDED_FOR];
} elseif (isset($_SERVER[HTTP_CLIENT_IP])) {
$realip = $_SERVER[HTTP_CLIENT_IP];
} else {
$realip = $_SERVER[REMOTE_ADDR];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
/*
* ----------------
* 操作完成的提示页面 Alan
* ----------------
*/
function get_show($url,$cont){
header("Location:../msage.php?url=$url&cont=$cont");
return;
}
function get_selected($type){
if($type==1){
$alan='selected';
}
return $alan;
}
/**
* 自动保存远程图片,到指定的$filename,如果$filename为空,则自动以时间为格式保存。
* @param $url 图片的完版url
* @param $filename 保存到文件名
*/
function GrabImage($url,$filename="") {
if($url==""):return false;endif;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=date("dMYHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
/**
* 自动获得远程网页内容
* @param unknown_type $url 远程url地址
*/
function get_url_content($url) {
for($changshinum=0;$changshinum<5;$changeshinum++){
try{
if (extension_loaded('curl')) {
if(strpos($url,':')){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
}else{
$content = file_get_contents($url);
}
} else {
$content = file_get_contents($url);
}
if(!$content){
die("获取地址:$url 内容出错.");
sleep(2);
continue;
}
break;
}catch(Exception $e)
{
sleep(2);
continue;
}
}
return $content;
}
/**
* 写入文件
* @param unknown_type $path 保存文件路径
* @param unknown_type $content 文件内容
* @param unknown_type $openType 文件方式 包括a,w,r,x 默认w
*/
function fileWrite($path,$content,$openType='w'){
try {
$fp = fopen($path, $openType);
fwrite($fp, $content);
fclose($fp);
} catch (Exception $e) {
die("写文件出错!".$path.$e);
}
}
#随机生成字符串
function randStr($length=8) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}