浏览器弹窗的安全限制主要在于咱们每每经过 JS 打开新窗口来实现下载
因此绕过安全弹窗问题的核心在于避免 JS 的窗口操做
具体的, 咱们能够经过建立一个_blank的超连接来返回文件流,实现文件下载php
$filename='xxx.pdf'; $encoded_filename = rawurlencode($filename); header('Content-type: application/force-download'); header("Content-Transfer-Encoding: binary"); if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua) || preg_match("/Edge/", $ua)) { header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); } else if (preg_match("/Firefox/", $ua)) { header("Content-Disposition: attachment; filename*=\"utf8''" . $file_name . '"'); } else { header('Content-Disposition: attachment; filename="' . $file_name . '"'); } header("Pragma:no-cache"); header("Expires:0"); readfile($filename);
两个逗号分隔的经纬度,bd09坐标系、单位米node
function lonlat2distance($lonlat1, $lonlat2) { if (empty($lonlat1) || empty($lonlat2)) return false; list($lon1, $lat1) = explode(',', $lonlat1); list($lon2, $lat2) = explode(',', $lonlat2); $er = 6378137; $radlat1 = M_PI * $lat1 / 180.0; $radlat2 = M_PI * $lat2 / 180.0; $radlong1 = M_PI * $lon1 / 180.0; $radlong2 = M_PI * $lon2 / 180.0; if ($radlat1 < 0) $radlat1 = M_PI / 2 + abs($radlat1);// south if ($radlat1 > 0) $radlat1 = M_PI / 2 - abs($radlat1);// north if ($radlong1 < 0) $radlong1 = M_PI * 2 - abs($radlong1);// west if ($radlat2 < 0) $radlat2 = M_PI / 2 + abs($radlat2);// south if ($radlat2 > 0) $radlat2 = M_PI / 2 - abs($radlat2);// north if ($radlong2 < 0) $radlong2 = M_PI * 2 - abs($radlong2);// west $x1 = $er * cos($radlong1) * sin($radlat1); $y1 = $er * sin($radlong1) * sin($radlat1); $z1 = $er * cos($radlat1); $x2 = $er * cos($radlong2) * sin($radlat2); $y2 = $er * sin($radlong2) * sin($radlat2); $z2 = $er * cos($radlat2); $d = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2) + ($z1 - $z2) * ($z1 - $z2)); $dist = acos(($er * $er + $er * $er - $d * $d) / (2 * $er * $er)) * $er; return intval($dist); }
/** * 客户端ip地址 * @param $type 0互联网地址xxx.xxx.xxx.xxx,1整型地址12345678 * @return mixed */ function getClientIp($type = 0){ $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return $ip[$type]; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown', $arr); if (false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } $long = sprintf("%u", ip2long($ip)); $ip = $long ? [$ip, $long] : ['0.0.0.0', 0]; return $ip[$type]; }
注解:
REMOTE_ADDR是可信的实际上的直接与服务器通讯的主机, 但客户端ip每每会被负载均衡节点或者代理服务器隔离
HTTP_* 系列变量是不可信的, 他们取自Http报文头部, 可被伪造。 若不考虑伪造, 则正常状况下能反映出客户端真实ipmysql
function unicodeDecode($str) { return preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function($match){return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');}, $str ); }
#基础示例 $ch = curl_init($url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_POST, true ); //curl_setopt($ch, CURLOPT_POSTFIELDS, $data); #上传文件时指定文件字段为: 文件名前面加上@前缀并使用完整路径 //curl_setopt($ch, CURLOPT_FILE, $file_handler); //指定curl拉取到数据的文件保存路径 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //curl_setopt($ch, CURLOPT_TIMEOUT, 500); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile.txt'); #指定请求须要带上的cookie的文件路径 //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookiefile.txt'); #指定响应返回的cookie存入的文件路径 $res = curl_exec($ch); curl_close($ch); #也可用array传入选项参数 curl_setopt_array($ch, array( CURLOPT_URL => 'http://www.example.com/', CURLOPT_RETURNTRANSFER => true, )); #基于curl作http请求封装 class Http { public function post($api, $data = array()) { return $this->http('get', $api, $data); } public function get($api, $data = array()) { return $this->http('post', $api, $data); } public function put($api, $data = array()) { return $this->http('put', $api, $data, array('X-HTTP-Method-Override: PUT')); } protected function http($type, $api, $data, $headers = array()) { switch ($type) { case 'post': $is_post = true; break; case 'get': case 'put': default: $is_post = false; break; } $data = (gettype($data) == 'array') ? http_build_query($data) : $data; $ch = curl_init(); if (!$is_post && $data) { $api .= "?{$data}"; } curl_setopt($ch, CURLOPT_URL, $api); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); if ($is_post) { curl_setopt($ch, CURLOPT_POST, $is_post); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $res = curl_exec($ch); curl_close($ch); return $res; } }
$config = array( 'host' => '127.0.0.1', 'port' => '3306', 'dbname' => 'xx', 'username' => 'xx', 'password' => 'xx', 'transaction' => true, 'pagesize' => 1000, ); set_time_limit(0); error_reporting(E_ALL); ini_set('log_errors', 'on'); ini_set('display_errors', true); ini_set('memory_limit', '1024M'); echo ("-- 数据处理开始 --"); try{ $dsn = "mysql:dbname={$config['dbname']};host={$config['host']};port={$config['port']}"; $dbh = new PDO($dsn, $config['username'], $config['password']); $dbh->exec('SET NAMES utf8'); $config['transaction'] AND $dbh->beginTransaction(); $dbh->exec('truncate table xxx'); $sthS = $dbh->prepare("select * from xxx limit ?, {$config['pagesize']}"); $sthI = $dbh->prepage('insert into yyy(a,b,c) values(?,?,?)'); $start = 0; while ($sthS->execute(array($start)) && $datas = $sthS->fetchAll(PDO::FETCH_OBJ)) { foreach ($datas as $data) { $sthI->execute(array( $data->a, $data->b, $data->c, )); } $start += $config['pagesize']; } $config['transaction'] AND $dbh->commit(); } catch (Exception $e) { $config['transaction'] AND $dbh->rollBack(); exit("-- 异常 --\n" . $e->getMessage()); } exit('-- 数据处理结束 --');
$xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA); $xml = simplexml_load_file($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA); $xml->getName() ->children() ->$node foreach ($xml as $element) {}
# root链接数据库来提权,从而下面mysql_get_server_info能读取数据库版本信息 mysql_connect(数据库ip, root帐号, root密码); $info = array( '操做系统' => PHP_OS, '服务器环境' => $_SERVER["SERVER_SOFTWARE"], 'PHP环境' => PHP_VERSION.'/'.php_sapi_name(), 'MySQL版本' => mysql_get_server_info(), '主机信息' => $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].' '.$_SERVER['SERVER_PROTOCOL'], 'WEB目录' => $_SERVER["DOCUMENT_ROOT"], '服务器域名/IP' => $_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]', '服务器时间' => date("Y年n月j日 H:i:s"), '北京时间' => gmdate("Y年n月j日 H:i:s",time()+8*3600), '执行时间限制' => ini_get('max_execution_time').'秒', '上传附件限制' => ini_get('upload_max_filesize'), '剩余空间' => round((disk_free_space(".")/(1024*1024)),2).'M', );
#当前脚本名 PHP_SELF #脚本的web路径 及 pathinfo信息 SCRIPT_NAME #脚本的web路径(CGI模式下则始终返回 /cgi-system/php.cgi ) REQUEST_URI #完整的请求路径 SCRIPT_FILENAME #脚本文件的绝对路径 其中 `__FILE__ == $_SERVER['DOCUMENT_ROOT'] . $SERVER['SCRIPT_NAME'] ;` #其余变量 SERVER_ADDR #服务器IP SERVER_NAME #服务器主机名 REMOTE_ADDR #客户机IP REMOTE_HOST #客户机主机名 QUERY_STRING #查询字符串 DOCUMENT_ROOT #WEB根目录
set_time_limit(0); error_reporting(E_ALL); ini_set('memory_limit', '1024M'); //ini_set('display_errors', true); $rawdir = 'images'; $savedir = 'watermarked_images'; $watermark_png = 'watermark.png'; //开始打水印 $watermark = imagecreatefrompng($watermark_png); list($waterW, $waterH) = getimagesize($watermark_png); $dir = opendir($rawdir); while (false !== ( $file = readdir ($dir))) { if ($file=='.' || $file=='..') continue; $path = sprintf('%s/%s/%s', realpath('.'), $rawdir, $file); $savepath = sprintf('%s/%s/%s', realpath('.'), $savedir, $file); list($imageW, $imageH) = getimagesize($path); $x = ($imageW - $waterW) / 2; $x = $x > 0 ? round($x) : 0; $y = ($imageH - $waterH) / 2; $y = $y > 0 ? round($y) : 0; $ext = pathinfo($path, PATHINFO_EXTENSION); switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($path); imagecopy($image, $watermark, $x, $y, 0, 0, $waterW, $waterH); imagejpeg($image, $savepath); break; case 'png': $image = imagecreatefrompng($path); imagecopy($image, $watermark, $x, $y, 0, 0, $waterW, $waterH); imagepng($image, $savepath); break; case 'gif': $image = imagecreatefromgif($path); imagecopy($image, $watermark, $x, $y, 0, 0, $waterW, $waterH); imagegif($image, $savepath); break; default: continue; break; } imagedestroy ($image); } closedir($dir); echo "\n\n\n图片打水印完毕\n\n\n";