PHP获取客户端真实IP地址多种方法

 更新时间:2016年11月25日 15:56  点击:1495

 //经过复杂的判断与算是的获取IP地址函数
 function getIP() {
        if (getenv('HTTP_CLIENT_IP')) {
                        $ip = getenv('HTTP_CLIENT_IP');
                }
                elseif (getenv('HTTP_X_FORWARDED_FOR')) {
                        $ip = getenv('HTTP_X_FORWARDED_FOR');
                }
                elseif (getenv('HTTP_X_FORWARDED')) {
                        $ip = getenv('HTTP_X_FORWARDED');
                }
                elseif (getenv('HTTP_FORWARDED_FOR')) {
                        $ip = getenv('HTTP_FORWARDED_FOR');

 

                }
                elseif (getenv('HTTP_FORWARDED')) {
                        $ip = getenv('HTTP_FORWARDED');
                }
                else {

                        $ip = $_SERVER['REMOTE_ADDR'];
                }
                return $ip;
 //最简单获取ip地址代码一句实例
 
 $reIP=$_SERVER["REMOTE_ADDR"];
 echo $reIP;
 
 //
 
 //php教程获取ip的算法
  if(getenv('HTTP_CLIENT_IP')) {
  $onlineip = getenv('HTTP_CLIENT_IP');
  } elseif(getenv('HTTP_X_FORWARDED_FOR')) {
  $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  } elseif(getenv('REMOTE_ADDR')) {
  $onlineip = getenv('REMOTE_ADDR');
  } else {
  $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
  }
  echo $onlineip;

  //可以分出内网与外网站ip地址获取程序
 
 function getip_out(){
 $ip=false;
 if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  $ip = $_SERVER["HTTP_CLIENT_IP"];
 }
 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  $ips教程 = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  for ($i = 0; $i < count($ips); $i++) {
   if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
    $ip = $ips[$i];
    break;
   }
  }
 }
 return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
 }
 
 echo getip_out();
 
 //php获取ip的算法,用了?号表达式来处理 
 
  $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
  $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"]; 
  

一般的记事本都是ansi格式哦,我们把它转换uft-8就必须输出时用\\\\\\\\xEF\\\\\\\\xBB\\\\\\\\xBF特。
 代码如下 复制代码
<?php
$f=fopen("test.txt", "wb");
$text=utf8_encode("a!");
//先用函数utf8_encode将所需写入的数据变成UTF编码格式。
$text="\xEF\xBB\xBF".$text;
//"\xEF\xBB\xBF",这串字符不可缺少,生成的文件将成为UTF-8格式,否则依然是ANSI格式。
fputs($f, $text);
//写入。
fclose($f);
?>

 

/*
array getimagesize ( string $filename [, array &$imageinfo ] )

getimagesize()函数将确定任何给定的图像大小的文件,并返回随着文件类型和高度/宽度的文本字符串是在一个正常的HTML IMG标签和相应的HTTP内容类型所使用的尺寸。
The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML I www.111cn.net MG tag and the correspondant HTTP content type

和getimagesize()也可以返回一些imageinfo参数的更多信息。

注意:请注意,少年警讯和JP2是有不同位深度组件的能力。在这种情况下,为“比特”的价值是最高的位深度的困难。此外,JP2上的JPEG文件可能包含多个2000 codestreams。在这种情况下,和getimagesize()返回第一个码流的价值是在文件的根接触。

注:有关资料检索图标从最高比特率图标。
*/

list($width, $height) = getimagesize($image);
$new_dimensions = resize_dimensions(300,400,$width,$height);

// Calculates restricted dimensions with a maximum of $goal_width by $goal_height
function resize_dimensions($goal_width,$goal_height,$width,$height) {
    $return = array('width' => $width, 'height' => $height);
   
    // If the ratio > goal ratio and the width > goal width resize down to goal width
    if ($width/$height > $goal_width/$goal_height && $width > $goal_width) {
        $return['width'] = $goal_width;
        $return['height'] = $goal_width/$width * $height;
    }
    // Otherwise, if the height > goal, resize down to goal height
    else if ($height > $goal_height) {
        $return['width'] = $goal_height/$height * $width;
        $return['height'] = $goal_height;
    }
   
    return $return;
}

/*
上面的函数我们就是利用

php 有个图片GD库getimagesize()函数。
有个函数是获取图片的基本信息。
getimagesize()
$img=getimagesize('图片源');
宽度为=$img[0];
高度为=$img[1];
格式为=$img[2];

如果你要简单的话可以更简单如

*/
$picpath = 'ww.111cn.net.gif';
$array = getimagesize($picpath);
print_r( $array );

echo '图片宽度为'.$array[0];
echo '图片高度为'.$array[1];
echo '图片格式为'.$array[2];

方法四
  //renumber
  $my_image = array_values(getimagesize('test.jpg'));
  //use list on new array
  list($width, $height, $type, $attr) = $my_image;

  //view new array
  print_r($my_image);

  //spit out content
  echo 'Attribute: '.$attr.'<br />';
  echo 'Width: '.$width.'<br />';

//这里面就会有图片的宽度与高度了

//再一个利用getimagesize显示缩略图的代码
function show_thumbnail($file)
{
    $max = 200 // Max. thumbnail width and height

    $size = getimagesize($file);

    if ( $size[0] <= $max && $size[1] <= $max )
    {
        $ret = '<img src="'.$file.'" '.$size[3].' border="0">';
    }
    else
    {
        $k = ( $size[0] >= $size[1] ) ? $size[0] / $max : $size[1] / $max;
        $ret = '<a href="javascript教程:;" onClick="window.open('image.php?img=';
        $ret .= $file.'','','width='.$size[0];
        $ret .= ',height='.$size[1].'')">';
        $ret .= '<img src="'.$file.'" width="'.floor($size[0]/$k).'" height="'.floor($size[1]/$k).'" border="0" alt="View full-size image"></a>';
    }

    return $ret;
}

 function getServerName()
 {
  $ServerName = strtolower($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:$_SERVER['HTTP_HOST']);
  if( strpos($ServerName,'http://') )
  {   
   return str_replace('http://','',$ServerName);
  }  
  return $ServerName;
 }
 
 //实例调用方法
 
 echo $getServerName;
 
 /*
 函数说明:
  strtolower 把字母转换成不写
  str_replace 替换指定的内容
  strpos 判断字符是否正在指定字符串中
  $_SERVER php全局变量
  本站原创教程,转载注明来源www.111cn.net
 */

function quickSort(&$data, $beg, $end) 

02 { 

03 if ($end > $beg) { 

04 $piv = $data[$beg]; 

05 $k = $beg + 1; 

06 $r = $end; 

07 while ($k < $r) { 

08 if ($data[$k] < $piv) { 

09 $k++; 

10 } else { 

11 $tmp = $data[$k]; 

12 $data[$k] = $data[$r]; 

13 $data[$r] = $tmp; 

14 $r--; 

15 } 

16 } 

17 if ($data[$k] >= $piv) { 

18 $k--; 

19 } 

20 $tmp = $data[$k]; 

21 $data[$k] = $data[$beg]; 

22 $data[$beg] = $tmp; 

23 quickSort($data, $beg, $k); 

24 quickSort($data, $r, $end); 

25 } 

26 } 

27   

28 function quickSort2(&$data) 

29 { 

30 $stack = array(); 

31 array_push($stack, array(0, count($data) - 1)); 

32 while (list($beg, $end) = array_pop($stack)) { 

33 if ($end > $beg) { 

34 $piv = $data[$beg]; 

35 $k = $beg + 1; 

36 $r = $end; 

37 while ($k < $r) { 

38 if ($data[$k] < $piv) { 

39 $k++; 

40 } else { 

41 $tmp = $data[$k]; 

42 $data[$k] = $data[$r]; 

43 $data[$r] = $tmp; 

44 $r--; 

45 } 

46 } 

47 if ($data[$k] >= $piv) { 

48 $k--; 

49 } 

50 $tmp = $data[$k]; 

51 $data[$k] = $data[$beg]; 

52 $data[$beg] = $tmp; 

53 array_push($stack, array($beg, $k)); 

54 array_push($stack, array($r, $end)); 

55 } 

56 } 

57 } 

58   

59 $data = array(7,6,6,3,8,1,8,1,9,1,3,11,51,1,25); 

60 //selectSort($data); 

61 //insertSort($data); 

62 quickSort2($data);

[!--infotagslink--]

相关文章