php 获得用户的真实IP地址

 更新时间:2016年11月25日 17:31  点击:2422
一个段经典的代码可以用来获取用户的真实IP地址,包括可以是内网或是外网,有需要的朋友可以参考一下。
 代码如下 复制代码
/**
 * 获得用户的真实IP地址
 *
 * @return  string
 */
function real_ip(){
    static $realip = NULL;
 
    if ($realip !== NULL){
        return $realip;
    }
 
    if (isset($_SERVER)){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip){
                $ip = trim($ip);
 
                if ($ip != 'unknown'){
                    $realip = $ip;
 
                    break;
                }
            }
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP'])){
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }
        else{
            if (isset($_SERVER['REMOTE_ADDR'])){
                $realip = $_SERVER['REMOTE_ADDR'];
            }
            else{
                $realip = '0.0.0.0';
            }
        }
    }
    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');
        }
    }
 
    preg_match("/[d.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
 
    return $realip;
}
一个简单的IP地址获取代码,不过这个只能是获取到服务器上的IP不能用在客户端哦,有需要的朋友参考下。
 代码如下 复制代码

 

/**
 * 获取服务器的ip
 * @access      public
 * @return string
 **/
function real_server_ip(){
 static $serverip = NULL;
 
 if ($serverip !== NULL){
  return $serverip;
 }
 
 if (isset($_SERVER)){
  if (isset($_SERVER['SERVER_ADDR'])){
   $serverip = $_SERVER['SERVER_ADDR'];
  }
  else{
   $serverip = '0.0.0.0';
  }
 }
 else{
  $serverip = getenv('SERVER_ADDR');
 }
 
 return $serverip;
}

文章也很简单我们只要用利用head 发送头部信息就能实现把文件下载到本了,有直接发送header信息也有把文件保存在服务器上再用header来发送哦,有需要的朋友参考下。

用的是表头方式下载

 代码如下 复制代码
Header( "Content-type:   application/octet-stream ");
Header( "Accept-Ranges:   bytes ");
Header( "Accept-Length:   ".filesize($file_dir   .   $file_name));
Header( "Content-Disposition:   attachment;   filename= "   .   $file_name);

判断文件存在再下载

 代码如下 复制代码
<?php
    if( isset( $_REQUEST["download"] ) )
    {
            $tfile = $_REQUEST["download"];
           
            if (file_exists($tfile))
            {
                $downfilename=substr($tfile,strrpos($tfile, "/")+1);
               //文件保存的名字可以修改为你需要的,可以和链接给的名字不一样
                // Prompt the user to download the new torrent file.
                header( "Content-type: application/octet-streamn" );
                header( "Content-disposition: attachment; filename=$downfilenamen" );
                header( "Content-transfer-encoding: binaryn");
                header( "Content-length: " . @filesize($tfile ) . "n" );
                // Send the torrent file
                $fp = @fopen( $tfile, "r" );
                @fpassthru( $fp );
                @fclose( $fp );
             }
           
            exit();
     }
?>

pdf文件下载

 代码如下 复制代码

<?php
$filename = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file/my_file.pdf";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/pdf"); //pdf格式的

?>


下面讲讲header中的Content-type:
不同的下载文件,对应不同的content-type,下面是大全:
PHP code
{
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk',
}

提供二款利用遍历批量修改文件的文件名的php代码,有需要的朋友可以参考一下。

实例一

 代码如下 复制代码
<?php
//利用PHP目录和文件函数遍历用户给出目录的所有的文件和文件夹,修改文件名称
function fRename($dirname){
 if(!is_dir($dirname)){
  echo "{$dirname}不是一个有效的目录!";
  exit();
 }
 $handle = opendir($dirname);
 while(($fn = readdir($handle))!==false){
  if($fn!='.'&&$fn!='..'){
   $curDir = $dirname.'/'.$fn;
   if(is_dir($curDir)){
    fRename($curDir);
   }
   else{
    $path = pathinfo($curDir);
    $newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];
    rename($curDir,$newname);   
    echo $curDir.'---'.$newname."<br>";   
   }
  }
 }
}
//给出一个目录名称调用函数
fRename('pl');
?>

实例二

 代码如下 复制代码

<?php
$dir = './';

if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
        if ($file == "." || $file == "..") continue;
        if(filetype($dir . $file) == 'file')
        {
            $newfile = str_replace('[1]', '', $file);
            rename($dir . $file, $dir . $newfile);
        }
    }
    closedir($dh);
}

提供一个实例的php自定义函数,这是一个可以批量修改文件后缀名的php程序,有需要的朋友可以参考一下。
 代码如下 复制代码

<?php
function foreachDir($path){
$handle=opendir($path);
if($handle){
while (false !== ($file = readdir($handle))) {
if($file!="." && $file!='..'){
if(is_dir($path.$file)){
echo $path.$file."<br/>";
foreachDir($path.$file);
}else{
echo "--".$path."/".$file."<br/>";
$ext = strripos($file,'.');
$aaa = substr($file,0,$ext);
rename($path.'/'.$file,$path.'/'.$aaa.'.JPG');
// die();
}
}
}
return false;
}
}
foreachDir('D:xampphtdocsTNF2');
[!--infotagslink--]

相关文章

  • php 获取用户IP与IE信息程序

    php 获取用户IP与IE信息程序 function onlineip() { global $_SERVER; if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP');...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • php根据用户语言跳转相应网页

    当来访者浏览器语言是中文就进入中文版面,国外的用户默认浏览器不是中文的就跳转英文页面。 <&#63;php $lan = substr(&#8194;$HTTP_ACCEPT_LANGUAGE,0,5); if ($lan == "zh-cn") print("<meta http-equiv='refresh' c...2015-11-08
  • php有效防止同一用户多次登录

    【问题描述】:同一用户在同一时间多次登录如果不能检测出来,是危险的。因为,你无法知道是否有其他用户在登录你的账户。如何禁止同一用户多次登录呢? 【解决方案】 (1) 每次登录,身份认证成功后,重新产生一个session_id。 s...2015-11-24
  • php ajax注册验证用户名是否存在代码

    这是注册程序是一款当用户输入完用户名是,就会自动去数据库中查询用户要注册的用户名是否己经被注册了,如果是返回提示否则提示可以注册。 conn.php文件 代...2016-11-25
  • js检测用户输入密码强度

    一个用Javascript检测用户输入密码强度的效果代码,以下代码主要是从以下四个方面检测用户输入的密码的强度的,有兴趣的朋友可以自己添加或修改成自己想要的形式! 1. 如果输入的密码位数少于5位,那么就判定为弱。 2. 如果...2015-10-23
  • 微信小程序用户授权最佳实践指南

    这篇文章主要给大家介绍了关于微信小程序用户授权最佳实践的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-08
  • 简单php cookie用户登录实例

    cookie 的用途之一是存储用户在特定网站上的密码和 id。另外,也用于存储起始页的首选项。在提供个人化查看的网站上,将要求阁下的网络浏览器利用阁下计算机硬驱上的少量...2016-11-25
  • PHP JS Ip地址及域名格式检测代码

    PHP IP地址格式检测函数复制代码 代码如下:function checkIp($ip){ $ip = str_replace(" ", "", $ip); $ip = strtolower($ip); $ip= str_replace("http://", "", $ip); $ip= str_replace("https://", ""...2013-10-04
  • MySQL验证用户权限的方法

    知识归纳因为MySQL是使用User和Host两个字段来确定用户身份的,这样就带来一个问题,就是一个客户端到底属于哪个host。 如果一个客户端同时匹配几个Host,对用户的确定将按照下面的优先级来排 基本观点越精确的匹配越优先...2015-11-08
  • sqlserver添加sa用户和密码的实现

    这篇文章主要介绍了sqlserver添加sa用户和密码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-07
  • 解决使用了nginx获取IP地址都是127.0.0.1 的问题

    这篇文章主要介绍了解决使用了nginx获取IP地址都是127.0.0.1 的问题,获取i工具的完整代码文中给大家提到,具体实例代码跟随小编一起看看吧...2021-09-18
  • mysql误删root用户恢复方法

    装完数据库清理一些默认账号的时候不小心把root删除了,flush privileges 之后的新 root 忘了grant任何权限,查看mysqld选项里面有个 &#8722;&#8722;skip-grant-tables复制代码 代码如下: #/usr/libexec/mysqld --verbos...2015-03-15
  • Win2012服务器 远程桌面帐户允许多用户同时登录的配置方法

    这篇文章主要介绍了Win2012服务器 远程桌面帐户允许多用户同时登录的配置方法,需要的朋友可以参考下...2016-11-01
  • 超详细的php用户注册页面填写信息完整实例(附源码)

    注册页面是大多数网站必备的页面,所以很有必要对自己的注册页面做些精心的设计。下面三张图,第一张是注册的展示页面,第二张思维导图就一个简单的逻辑,第三张是通过firebug查看调用的JS文件。 一、给每个输入框写下说明在...2015-11-24
  • 如何有效提高网站的用户回头率

    第一,网站的内容;请各位站长朋友不要一天到晚只想着出什么好的绝招来推广网站,却忽略了网站的内容;其实网站的内容是极为重要的,因为这是你的本,你的根!网站的内容只有不断...2017-07-06
  • 关于JSP用户登录连接数据库详情

    这篇文章主要介绍了关于JSP用户登录连接数据库的相关资料,需要的朋友可以参考下面文章内容...2021-09-07
  • 如何判断用户是否访问过某个网址

    一位站长译的一个国外的如何判断用户是否访问过某个网址文章,个人感觉写得非常不错,下面分享一下。 我们经常有这样的需求:想知道用户之前有没有访问过某个网址。有...2016-09-20
  • php 获得用户的真实IP地址

    一个段经典的代码可以用来获取用户的真实IP地址,包括可以是内网或是外网,有需要的朋友可以参考一下。 代码如下 复制代码 /** * 获得用户的...2016-11-25
  • 在VMware虚拟机中查看Linux的IP地址的方法

    这篇文章主要介绍了在VMware虚拟机中查看Linux的IP地址的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-16