备份下代码
<?php namespace Lib\Library; use Closure; use Exception; /** * 文件锁 * 1.阻塞,进程加锁时发现其他进程已经加锁会一直阻塞等待,一直等到可以加锁成功,然后去执行业务代码 * 2.非阻塞,进程加锁时发现其他进程已经加锁直接返回,不会执行业务代码 * 3.注意Erp默认Session驱动为文件,本身请求已经存Session锁机制,所以测试非阻塞时请修改Session驱动为Redis * @package Lib\Library */ class FileLock { /** * 锁文件位置 * @var string */ private $file; /** * 锁文件句柄 * @var */ private $fileHandle; /** * 构造函数 * @param string $name 锁名称 * @throws Exception */ public function __construct($name = 'lock') { //初始化文件 $path = RUNTIME_PATH . 'Lock' . DIRECTORY_SEPARATOR; $this->file = $path . md5($name); if (!is_dir($path)) { if (!mkdir($path, 0777, true)) { throw new Exception('文件锁目录创建失败,请检查目录' . $path . '是否可写'); } } if (!file_exists($this->file)) { if (file_put_contents($this->file, '')) { throw new Exception('文件锁创建失败,请检查文件' . $this->file . '是否可写'); } } } /** * 加锁 * @param bool $block * @return bool */ public function lock($block = true) { $this->fileHandle = fopen($this->file, 'r'); $is_flock = $block ? flock($this->fileHandle, LOCK_EX) : flock($this->fileHandle, LOCK_EX | LOCK_NB); if (!$is_flock) { fclose($this->fileHandle); } return $is_flock; } /** * 释放 */ public function release() { if (!$this->fileHandle) { return false; } $unlock = flock($this->fileHandle, LOCK_UN); if ($unlock) { fclose($this->fileHandle); $this->fileHandle = null; } return $unlock; } /** * 加锁执行 * @param Closure $func 执行闭包函数 * @param bool $block 是否阻塞 * @return mixed 闭包函数的返回值 */ public function lockToExecute($func, $block = true) { $is_flock = $this->lock($block); $call_back = null; if ($is_flock) { $call_back = $func(); $this->release(); } return $call_back; } }
1、允许单个域名访问指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:header('Access-Control-Allow-Origin:http://client....
header() 函数向客户端发送原始的 HTTP 报头。(官方解释)通俗的讲header函数将参数中的字符串作为服务端的响应头来返回给客户端。什么是服务端的响应头?打开谷歌浏览器看看network中的请求response header信息即可。更多的参数百度response header即可浏览器...
开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...
md5/sha1+salt方式是目前各大cms常用的加密方式,虽然salt安全,但是各大md5网站也在研究这个方向,那么我们应该选择password_hash动态hash来助力,一种密码有多种hash结果.看代码模拟登陆.<?php //01.注册 $user ='zhang...
逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event { /** &nbs...
php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int { &nb...