php常用的一些代码

1.获取用户真实ip

 1 function getIP()
 2 {
 3     if (getenv("HTTP_X_FORWARDED_FOR")) {
 4         // 这个提到最前面,作为优先级,nginx代理会获取到用户真实ip,发在这个环境变量上,必须要nginx配置这个环境变量HTTP_X_FORWARDED_FOR
 5         $ip = getenv("HTTP_X_FORWARDED_FOR");
 6     } else {
 7         if (getenv("REMOTE_ADDR")) {
 8             // 在nginx作为反向代理的架构中,使用REMOTE_ADDR拿到的将会是反向代理的的ip,即拿到是nginx服务器的ip地址。往往表现是一个内网ip。
 9             $ip = getenv("REMOTE_ADDR");
10         } else { 
11             //getenv不支持IIS的isapi方式运行的php
12             //通过$_SERVER获取
13             if ($_SERVER['REMOTE_ADDR']) {
14                 $ip = $_SERVER['REMOTE_ADDR'];
15             } else { 
16                 if (getenv("HTTP_CLIENT_IP")) {
17                     // HTTP_CLIENT_IP攻击者可以伪造一个这样的头部信息,导致获取的是攻击者随意设置的ip地址。
18                     $ip = getenv("HTTP_CLIENT_IP");
19                 } else {
20                     $ip = "unknown";
21                 }
22             }
23         }
24     }
25     return $ip;
26 }

参考一下 http://www.cnblogs.com/wangtao_20/p/4582701.html 完善一下获取ip的方法 

2.curl实现post、get抓取数据

3.遍历目录下所有文件和子文件夹

 1 <?php
 2  function read_all_dir ( $dir )
 3     {
 4         $result = array();
 5         $handle = opendir($dir);
 6         if ( $handle )
 7         {
 8             while ( ( $file = readdir ( $handle ) ) !== false )
 9             {
10                 if ( $file != '.' && $file != '..')
11                 {
12                     $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
13                     if ( is_dir ( $cur_path ) )
14                     {
15                         $result['dir'][$cur_path] = read_all_dir ( $cur_path );
16                     }
17                     else
18                     {
19                         $result['file'][] = $cur_path;
20                     }
21                 }
22             }
23             closedir($handle);
24         }
25         return $result;
26     }
27 ?>
原文地址:https://www.cnblogs.com/lingshao/p/5628273.html