46 个非常有用的 PHP 代码片段(3)

10. 获取用户的真实  IP

function getRealIpAddr() { if (!emptyempty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; }

语法:

<?php $ip = getRealIpAddr(); echo $ip; ?>


11. 转换 URL:从字符串变成超链接
如果你正在开发论坛,博客或者是一个常规的表单提交,很多时候都要用户访问一个网站。使用这个函数,URL 字符串就可以自动的转换为超链接。

function makeClickableLinks($text) { $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)', '<a href="https://www.jb51.net/article/\1">\1</a>', $text); $text = eregi_replace('([[:space:]()[{}])([-a-zA-Z0-9@:%_+.~#?&//=]+)', '\1<a href="https://\2">\2</a>', $text); $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})', '<a href="https://www.jb51.net/mailto:\1">\1</a>', $text); return $text; } 语法: <?php $text = "This is my first post on "; $text = makeClickableLinks($text); echo $text; ?>


12. 阻止多个 IP 访问你的网站
这个代码片段可以方便你禁止某些特定的 IP 地址访问你的网站

if ( !file_exists('blocked_ips.txt') ) { $deny_ips = array( '127.0.0.1', '192.168.1.1', '83.76.27.9', '192.168.1.163' ); } else { $deny_ips = file('blocked_ips.txt'); } // read user ip adress: $ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : ''; // search current IP in $deny_ips array if ( (array_search($ip, $deny_ips))!== FALSE ) { // address is blocked: echo 'Your IP adress ('.$ip.') was blocked!'; exit; }


13. 强制性文件下载
如果你需要下载特定的文件而不用另开新窗口,下面的代码片段可以帮助你。

function force_download($file) { $dir = "../log/exports/"; if ((isset($file))&&(file_exists($dir.$file))) { header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' . $dir.$file . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($dir.$file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$dir$file"); } else { echo "No file selected"; } }


语法:

<php force_download("image.jpg"); ?>


14. 创建 JSON 数据
使用下面的 PHP 片段可以创建 JSON 数据,可以方便你创建移动应用的 Web 服务

$json_data = array ('id'=>1,'name'=>"Mohit"); echo json_encode($json_data);


15. 压缩 zip 文件
使用下面的 PHP 片段可以即时压缩 zip 文件

function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close(); //check to make sure the file exists return file_exists($destination); } else { return false; } } 语法: <?php $files=array('file1.jpg', 'file2.jpg', 'file3.gif'); create_zip($files, 'myzipfile.zip', true); ?>


16. 解压文件

function unzip($location,$newLocation) { if(exec("unzip $location",$arr)){ mkdir($newLocation); for($i = 1;$i< count($arr);$i++){ $file = trim(preg_replace("~inflating: ~","",$arr[$i])); copy($location.'https://www.jb51.net/'.$file,$newLocation.'https://www.jb51.net/'.$file); unlink($location.'https://www.jb51.net/'.$file); } return TRUE; }else{ return FALSE; } }



语法:

<?php unzip('test.zip','unziped/test'); //File would be unzipped in unziped/test folder ?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6b63572ee0f0c42def825373c5cfbc86.html