public static function getIpX(): ?string {
[$ip] = self::getIp();
return $ip;
}
public static function getIp(): array {
$ip0 = $ip = $_SERVER['REMOTE_ADDR'] ?? null;
if (in_array($ip, [
'10.29.185.7', '127.0.0.1', '172.17.0.1', '172.31.242.237', # 可信IP列表
])) {
$ip1 = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null;
if ($ip1) {
$ip0 = $ip1;
$ip = explode(',', $ip1)[0];
}
}
if (!$ip) {
$ip = $_SERVER['REMOTE_ADDR'] ?? null;
}
return [$ip, $ip0];
}
分类目录归档:PHP
Nginx+PHP服务器上传限制调节
nginx:
client_max_body_size 1G;
php.ini:
upload_max_filesize = 1G
post_max_size = 1G
PHP简易排队限流实现
$fp = fopen(sprintf("tmp/wk_exam_examination.%d.lock", $userid % 10), "w");
if (!flock($fp, LOCK_EX | LOCK_NB)) {
?>
<style>
#info {
text-align: center;
margin: 50px 0;
}
#info td {
font-size: 36px;
color: seagreen;
}
</style>
<div id='info'></div>
<script>
let sec = 5000
setTimeout(function () {
window.location.reload()
}, sec)
setInterval(function () {
sec -= 100
if (sec >= 0) {
let ok = sec / 1000
document.getElementById('info').innerHTML = `<table style="margin: 0 auto;">
<tr><td style='width: 50%; text-align: right;'>页面排队中...</td>
<td style="width: 2em; text-align: center;">${ok}</td><td>秒后将重试!</td></tr></table>`;
}
}, 100)
</script><?php
die;
}
queue:listen queue:work的区别
queue:listen 是热加载,效率低,修改job类后会生效
queue:work 不会热加载,效率高,修改job类后需要重启才能生效
Warning: Swoole\Coroutine::create(): exceed max number of coroutine 100000
Warning: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in Unknown on line 0
__construct、register_shutdown_function、__destruct执行顺序
run __construct
run shutdown_function
run __destruct
* unset 等操作可以提前触发 __destruct
可以利用变量作用域在__destruct内来释放连接、解锁等释放操作
启用 opcache.enable_cli 也需要小心
opcache.enable_cli + xdebug + swoole 可能会导致“段错误”(Segmentation fault)