当空字符串使用 explode() 函数时 返回为 Array ( [0] => ) 而非 Array ( )
相应的 count() 值为 1 而非 0
学习日志 LIUGUOFENG
<?php
//根据IP地址获取其地理位置(国家,省份,城市等)的方法
function GetIpLookup($ip = ''){
if(empty($ip)){
return '请输入IP地址';
}
$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
if(empty($res)){
return false;
}
$jsonMatches = array();
preg_match('#\{.+?\}#', $res, $jsonMatches);
if(!isset($jsonMatches[0])){
return false;
}
$json = json_decode($jsonMatches[0], true);
if(isset($json['ret']) && $json['ret'] == 1){
$json['ip'] = $ip;
unset($json['ret']);
}else{
return false;
}
return $json;
}
$ipInfos = GetIpLookup('192.30.253.112'); //测试 github.com 的IP地址
echo '<pre>';
print_r($ipInfos);
echo '</pre>';
PHP 获取当前域名有两个变量 HTTP_HOST 和 SERVER_NAME:
相同点:
当满足以下三个条件时,两者会输出相同信息。
1. 服务器为 80 端口
2. apache 的 conf 中 ServerName 设置正确
3. HTTP/1.1 协议规范
不同点:
1. 通常情况:
$_SERVER["HTTP_HOST"] 在 HTTP/1.1 协议规范下,会根据客户端的 HTTP 请求输出信息。
$_SERVER["SERVER_NAME"] 默认情况下直接输出 apache 的配置文件 httpd.conf 中的 ServerName 值。
2. 当服务器为非 80 端口时:
$_SERVER["HTTP_HOST"] 会输出端口号,例如:guofeng.io:8080
$_SERVER["SERVER_NAME"] 会直接输出 ServerName 值
因此在这种情况下,可以理解为:HTTP_HOST = SERVER_NAME : SERVER_PORT
3. 当配置文件 httpd.conf 中的 ServerName 与 HTTP/1.0 请求的域名不一致时:
httpd.conf 配置如下:
<virtualhost *> ServerName mimiz.cn ServerAlias blog.liuguofeng.com </virtualhost>
客户端访问域名 blog.liuguofeng.com
$_SERVER["HTTP_HOST"] 输出 blog.liuguofeng.com
$_SERVER["SERVER_NAME"] 输出 guofeng.io
所以,在实际程序中,应尽量使用 $_SERVER["HTTP_HOST"] ,比较可靠。
另:
"PHP_SELF"
当前正在执行脚本的文件名,与 document root 相关。举例来说,在 URL 地址为 https://blog.liuguofeng.com/wp-admin/post-new.php 的脚本中使用 $_SERVER['PHP_SELF'] 将会得到 /wp-admin/post-new.php 这个结果。__FILE__ 常量包含当前(例如包含)文件的绝对路径和文件名。
"SCRIPT_NAME"
包含当前脚本的路径。这在页面需要指向自己时非常有用。__FILE__ 包含当前文件的绝对路径和文件名(例如包含文件)。
研究代码时发现个有意思的现象
逻辑运算符中的 "并且 &&" 和 "或者 ||"
当 && 前为 false 时,无需执行后面语句即可返回结果 false;
当 && 前为 true 时,需执行后面,若后面为 true 则结果返回 true,若后面为 false 则结果返回 false;
当 || 前为 true 时,无需执行后面语句即刻返回结果 true;
当 || 前为 false 时,需执行后面,若后面为 true 则结果返回 true,若后面为 false 则结果返回 false;
由于逻辑运算符 && || 的优先级高于赋值运算符 =
以 $b=0 || $c=2 为例
|| 的优先级高一些,先算 0 || $c
由于 $c = 2,所以 即 0 || 2
返回 true,true 就赋给了$b
所 $b = true
echo $b 就等于 1
public function test(){
$this->forceDownload("/Uploads/Attachment/test.pdf");
}
function forceDownload($filename) {
// if (false == file_exists($filename)) {
// return false;
// }
// http headers
header('Content-Type: application-x/force-download');
header('Content-Disposition: attachment; filename="' . basename($filename) .'"');
header('Content-length: ' . filesize($filename));
// for IE6
if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')) {
header('Cache-Control: no-cache, must-revalidate');
}
header('Pragma: no-cache');
// read file content and output
return readfile($filename);
}
<?php
$test = [
'a' => [
'mobile' => '13333333333',
'other' => 'aaaaaaaaaaaa'
],
'b' => [
'mobile' => '14444444444',
'other' => 'bbbbbbbbbb'
],
'c' => [
'mobile' => '14444444444',
'other' => 'cccccccccc'
],
];
$result = [];
foreach ($test as $k => $v) {
$result[$v['mobile']][] = $v;
}
echo "<pre>";
print_r($test);
echo "</pre>";
echo "<pre>";
print_r($result);
echo "</pre>";
?>
返回结果:
Array
(
[a] => Array
(
[mobile] => 13333333333
[other] => aaaaaaaaaaaa
)
[b] => Array
(
[mobile] => 14444444444
[other] => bbbbbbbbbb
)
[c] => Array
(
[mobile] => 14444444444
[other] => cccccccccc
)
)
Array
(
[13333333333] => Array
(
[0] => Array
(
[mobile] => 13333333333
[other] => aaaaaaaaaaaa
)
)
[14444444444] => Array
(
[0] => Array
(
[mobile] => 14444444444
[other] => bbbbbbbbbb
)
[1] => Array
(
[mobile] => 14444444444
[other] => cccccccccc
)
)
)
传入''
返回 string(0) ''
<pre> string(0) "" </pre>
传入 '0'
返回 string(1) '0'
<pre> string(1) "0" </pre>
按网上的教程一直出现找不到 QRcode 问题, 按下面的结构和代码写就可以了
public function qrcode($url='https://blog.liuguofeng.com/',$level=3,$size=4){
Vendor('phpqrcode.phpqrcode');
$errorCorrectionLevel =intval($level) ;//容错级别
$matrixPointSize = intval($size);//生成图片大小
//生成二维码图片
//echo $_SERVER['REQUEST_URI'];
$object = new \QRcode();
$object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
}
Continue reading thinkphp 3.2 使用 PHPQRCODE 生成二维码 找不到 QRcode 问题
使用以下方法获取:
$put=file_get_contents('php://input');
$put=json_decode($put,1);
foreach ($put as $key => $value) {}