时空云博客 - php
type: channel
link: https://blog.skyw.cc/tag/php/
items: 记录php防跨站
items: 谷歌api+Redis缓存实现php获取网站favicon图标api
items: php base64解码出现乱码
items: php区分0，null和空值
items: 解决php对数组进行json编码很慢的问题
items: PHP500错误 快速定位方法

记录php防跨站
type: item
date: 2022-06-20T22:14:54+08:00
description: 最近安装一款websocket的客服系统时，使用了以下php.ini[PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; PHP's initialization file, generally called php.ini, is responsible for ; configuring many of the aspects of PHP's behavior. ; PHP attempts to find and load this configuration from a number of locations. ; The following is a summary of its search order: ; 1. SAPI module specific location. ; 2. The PHPRC environment variable. (As of PHP 5.2.0) ; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0) ; 4. Current working directory (except CLI) ; 5. The web server's directory (for SAPI modules), or directory of PHP ; (otherwise in Windows) ; 6. The directory from the --with-config-file-path compile time option, or the ; Windows directory (C:\windows or C:\winnt) ; See the PHP docs for more specific information. ; http://php.net/configuration.file ; The syntax of the file is extremely simple. Whitespace and lines ; beginning with a semicolon are silently ignored (as you probably guessed). ; Section headers (e.g. [Foo]) are also silently ignored, even though ; they might mean something in the future. ; Directives following the section heading [PATH=/www/mysite] only ; apply to PHP files in the /www/mysite directory. Directives ; following the section heading [HOST=www.example.com] only apply to ; PHP files served from www.example.com. Directives set in these ; special sections cannot be overridden by user-defined INI files or ; at runtime. Currently, [PATH=] and [HOST=] sections only work under ; CGI/FastCGI. ; http://php.net/ini.sections ; Directives are specified using the following syntax: ; directive = value ; Directive names are *case sensitive* - foo=bar is different from FOO=bar. ; Directives are variables used to configure PHP or PHP extensions. ; There is no name validation. If PHP can't find an expected ; directive because it is not set or is mistyped, a default value will be used. ; The value can be a string, a number, a PHP constant (e.g. E_ALL or…

谷歌api+Redis缓存实现php获取网站favicon图标api
type: item
date: 2022-02-06T19:29:00+08:00
description: 前言：我自己的导航站懒得添加站点时一个一个去复制粘贴他们的图标地址，网上找的接口和代码不是很好用，于是有了这个作品本代码需要有redis才可使用，需要搭建在海外服务器上（因为调用的谷歌api）使用方法：api.php?url=对方网页地址fav_function.php<?php $redis = new Redis(); if (!$redis->connect('127.0.0.1', 6379, 5)) die('Redis无法链接，请联系上级重启Redis'); $redis->select(1); //选择数据库 function redis_search($key) { global $redis; $json = $redis->get($key); if ($json == '') { return false; } elseif ($array = json_decode($json, true)) { return $array; } else { return $json; } } function redis_save($key, $data, $limit = 600) { global $redis; $json = is_array($data) ? json_encode($data) : $data; $redis->setex($key, $limit, $json); } function redis_del($key) { global $redis; $redis->del($key); } function pr($a) { print_r($a); die; } function restoreUrl($shortUrl) { //pr($shortUrl); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $shortUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_NOBODY, false); curl_setopt($curl, CURLOPT_TIMEOUT, 15); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); $data = curl_exec($curl); $curlInfo = curl_getinfo($curl); curl_close($curl); if ($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) { return $curlInfo['redirect_url']; } return $shortUrl; } function geticon($domain) { $key = 'icon:'.$domain; if ($data = redis_search($key)) return $data; $url = 'https://www.google.com/s2/favicons?domain='.$domain; $data = curl_get(restoreUrl($url)); redis_save($key, $data, 6000); echo $data; } function curl_get($url) { $ch = curl_init($url); $httpheader[] = "Accept: */*"; $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8"; $httpheader[] = "Connection: close"; curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,…

php base64解码出现乱码
type: item
date: 2022-01-28T21:54:00+08:00
description: 今天帮小龙改写订单导出时，我写的ajax先输出base64，再由js提交ajax到php文件让php解码base64输出下载文件，结果输出的base64我网上解密正常，服务端解码乱码。于是一番百度发现是$_GET传输时+被替换成空格造成的，改回来即可$str = base64_decode(str_replace(" ","+",$_GET['str']));

php区分0，null和空值
type: item
date: 2021-07-22T16:51:00+08:00
description: 今天修改数据库相关的php函数时用到了if ($v=='')来判断是否为空值，如果为空就跳过结果发现数字型的0也给跳过了于是百度发现了以下方法区分，特此记录<?php $a = 0; $b = ''; $c = null; // 区分 0、''、null // 1、方法一 if ($a !== '') { echo '0和null'; } else { echo '空字符串'; } // 2、方法二 if ($a !== null) { echo '0和空字符串'; } else { echo 'null'; } // 3、方法三 if (strlen($a) > 0) { echo '0'; } else { echo '空字符串和null'; } // 以上三种方法都只能把其中一个和另外两个区分出来，如果需要3个单独区分，需要结合使用

解决php对数组进行json编码很慢的问题
type: item
date: 2021-07-16T20:57:25+08:00
description: 前言今天有人跟我说直客对接不了我们社区，经过检查发现是获取商品列表超时了一开始我以为是mysql处理慢的原因，于是用redis进行了缓存数据，结果还是很慢要30多s才能出400个商品然后我就断点测试速度，发现在json_encode前花的时间其实只有1s左右，那么说明是json_encode太慢导致的了然后我百度如何让json_encode更快，有人给出拼接出来的方案，经过尝试，确实快了很多，只要10s左右了

PHP500错误 快速定位方法
type: item
date: 2021-06-03T11:52:00+08:00
description: 下面代码放在程序最开始。可以快速定位错误文件、行数、原因<?php register_shutdown_function(function(){ print_r(error_get_last()); });