摘要:加密解密类由于开始废弃传统的加密函数而改用,因此该类支持的版本为简易加解密类通用秘钥所有可用的加密类型可参考构造函数加密类型。
加密解密类
由于 PHP7.1 开始废弃传统的加密函数 mcrypt_encrypt 而改用 openssl_encrypt,因此该类支持的 PHP 版本为: (PHP 5 >= 5.3.0, PHP 7)
</>复制代码
METHOD = $method;
}
}
/**
* 加密字符串
*
* @param String 待加密数据
* @param string 加密秘钥,若为空,则使用通用秘钥
* @return void
*/
public function encrypt($input, $key = "") {
if (empty($key)) $key = $this->KEY;
$data = openssl_encrypt($input, $this->METHOD, $key, OPENSSL_RAW_DATA);
$data = base64_encode($data);
return $data;
}
/**
* 解密字符串
*
* @param String 待解密字符串
* @param string 解密秘钥,若为空,则使用通用秘钥
* @return void
*/
public function decrypt($input, $key = "") {
if (empty($key)) $key = $this->KEY;
$data = openssl_decrypt(base64_decode($input), $this->METHOD, $key, OPENSSL_RAW_DATA);
return $data;
}
}
发送邮件类
先下载 PHPMailer,解压后,将其放置你的项目中,并修改类中的引入路径。
</>复制代码
_init();
$this->_baseURL = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"];
}
private function _init() {
if ($this->_email == null) {
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = get_global_config("mail.Host");
$mail->SMTPAuth = true;
$mail->Username = get_global_config("mail.Username");
$mail->Password = get_global_config("mail.Password");
$mail->SMTPSecure = get_global_config("mail.SMTPSecure");
$mail->Port = get_global_config("mail.Port");
$mail->setFrom(get_global_config("mail.Username"), get_global_config("mail.Fromname"));
$this->_email = $mail;
return true;
} catch (Exception $e) {
// return null;
return false;
}
}
return true;
}
public function sendEmail($toEmail, $toUsername, $subject, $body) {
if (!$this->_email) return false;
try {
$this->_email->addAddress($toEmail, $toUsername);
$this->_email->isHTML(true);
$this->_email->Subject = $subject;
$this->_email->Body = $body;
// 当邮件不支持 HTML 时显示的邮件内容
$this->_email->AltBody = $body;
$this->_email->send();
// echo "Message has been sent";
return true;
} catch (Exception $e) {
return false;
// echo "Message could not be sent.";
// echo "Mailer Error: " . $this->_email->ErrorInfo;
}
}
}
模拟锁
使用 Redis 模拟进程锁,参考至 discuz 的进程锁源码。
</>复制代码
cache = Roc::redis();
$this->cache->select(Roc::get("redis.db"));
}
/**
* 是否被锁
*
* @param string 锁名
* @param integer 上锁时间
* @return boolean
*/
public function islocked($process, $ttl = 0) {
$ttl = $ttl < 1 ? 600 : intval($ttl);
return $this->_status("get", $process) || $this->_find($process, $ttl);
}
/**
* 解锁
*
* @param string 锁名
* @return void
*/
public function unlock($process) {
$this->_status("rm", $process);
$this->_cmd("rm", $process);
}
private function _status($action, $process) {
static $plist = array();
switch ($action) {
case "set" : $plist[$process] = true; break;
case "get" : return !empty($plist[$process]); break;
case "rm" : $plist[$process] = null; break;
case "clear" : $plist = array(); break;
}
return true;
}
private function _find($name, $ttl) {
if(!$this->_cmd("get", $name)) {
$this->_cmd("set", $name, $ttl);
$ret = false;
} else {
$ret = true;
}
// $this->_status("set", $name);
return $ret;
}
private function _cmd($cmd, $name, $ttl = 0) {
return $this->_process_cmd_memory($cmd, $name, $ttl);
// static $allowmem;
// if($allowmem === null) {
// $allowmem = Roc::get("redis.db");
// }
// if($allowmem) {
// return self::_process_cmd_memory($cmd, $name, $ttl);
// } else {
// return false;
// // return self::_process_cmd_db($cmd, $name, $ttl);
// }
}
private function _process_cmd_memory($cmd, $name, $ttl = 0) {
$ret = "";
$name = "process_lock_".$name;
switch ($cmd) {
case "set" :
$ret = $this->cache->setex($name, time(), $ttl);
// $ret = memory("set", "process_lock_".$name, time(), $ttl);
break;
case "get" :
$ret = $this->cache->get($name);
// $ret = memory("get", "process_lock_".$name);
break;
case "rm" :
$ret = $this->cache->delete($name);
// $ret = memory("rm", "process_lock_".$name);
}
echo $cmd . ":";
var_dump($ret);
return $ret;
}
private function _process_cmd_db($cmd, $name, $ttl = 0) {
$ret = "";
switch ($cmd) {
case "set":
$ret = C::t("common_process")->insert(array("processid" => $name, "expiry" => time() + $ttl), FALSE, true);
break;
case "get":
$ret = C::t("common_process")->fetch($name);
if(empty($ret) || $ret["expiry"] < time()) {
$ret = false;
} else {
$ret = true;
}
break;
case "rm":
$ret = C::t("common_process")->delete_process($name, time());
break;
}
return $ret;
}
}
Redis
一个简易的 Redis 操作类,支持在查找不到缓存时,进行相对应的回调操作。
</>复制代码
cache = new Redis();
}
// ============= String 操作
/**
* 设置缓存
*
* @param String 缓存key值
* @param String/Object 缓存数据,可为字符串可为数组。
* @param Int 过期时间
* @return Boolean
*/
public function set($key, $value, $ttl = null) {
if (is_array($value)) $value = serialize($value);
if ($ttl !== null && $ttl > 0)
return $this->cache->setex($this->_key($key), $ttl, $value);
else
return $this->cache->set($this->_key($key), $value);
}
/**
* 获取缓存数据
*
* @param String 值
* @param Data 默认值,设置后,如果缓存没有数据则返回默认值
* @return Data
*/
public function get($key, $callback = false) {
$res = $this->cache->get($this->_key($key));
if ($res === false && $callback === true) {
$method_name = "_$key";
if (method_exists($this, $method_name)) {
return $this->$method_name();
} else {
return false;
}
}
// 如果反序列化成功,则返回反序列化的数据。
// @防止报 warning。
if (@unserialize($res) !== false) return unserialize($res);
return $res;
}
/**
* 删除缓存
*
* @param String 需要删除的键名
* @return true or false
*/
public function rm($key) {
return $this->cache->delete($this->_key($key));
}
public function inc($key, $step = 1) {
return $this->cache->incr($this->_key($key), $step);
}
public function dec($key, $step = 1) {
return $this->cache->decr($this->_key($key), $step);
}
private function _key($key) {
return $this->prefix . $key;
}
public function getMultiple($keys) {
$_keys = [];
foreach ($keys as $key) $_keys[] = $this->_key($key);
return $this->cache->getMultiple($_keys);
}
// ============== Hash 操作
public function hset($hash, $key, $value) {
return $this->cache->hSet($hash, $key, $value);
}
public function hget($hash, $key) {
return $this->cache->hGet($hash, $key);
}
public function hgetall($hash) {
return $this->cache->hGetAll($hash);
}
public function hvals($hash) {
return $this->cache->hVals($hash);
}
public function hkeys($hash) {
return $this->cache->hKeys($hash);
}
// ================ 通用回调
// Your callback code here.
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/26208.html
摘要:请求未通过的验证时会抛出此异常。异常处理是非常重要但又容易让开发者忽略的功能,这篇文章简单解释了内部异常处理的机制以及扩展异常处理的方式方法。 异常处理是编程中十分重要但也最容易被人忽视的语言特性,它为开发者提供了处理程序运行时错误的机制,对于程序设计来说正确的异常处理能够防止泄露程序自身细节给用户,给开发者提供完整的错误回溯堆栈,同时也能提高程序的健壮性。 这篇文章我们来简单梳理一下...
摘要:介绍下一个新项目,后端该如何从零去搭建。我们先假设这个项目由两部组成提供给站点使用的提供给运营人员使用的管理后台。因此通过回顾,我们得出我们的后端项目需要一个的层次,来存放业务逻辑。 这是 后端开发者从零做一个移动应用 的后端部分第二篇。介绍下一个新项目,后端该如何从零去搭建。我们先假设这个项目由两部组成 提供给wap站点、app使用的api; 提供给运营人员使用的管理后台。 整个...
摘要:,跨站脚本攻击。实际发的请求就是,用于表示这是一个请求。,用于告知服务器根据这个参数获取回调函数的名称,通常约定就叫。,回调函数的名称,也是前面参数的值,可省略,会自动生成。 本次课程主要围绕 PHP 面试和笔试中经常会出现的一些知识点,但是面试官会在笔试题基础上深入扩展,那么你知道如何更好的回答让面试官满意吗?题目收集自腾讯,迅雷,美图等公司的笔试面试题,以及本人面试经历中印象中的知...
摘要:入门,第一个这是一门很新的语言,年前后正式公布,算起来是比较年轻的编程语言了,更重要的是它是面向程序员的函数式编程语言,它的代码运行在之上。它通过编辑类工具,带来了先进的编辑体验,增强了语言服务。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不觉已经到来了,总结过去的 2017,相信小伙们一定有很多收获...
阅读 3222·2021-11-23 09:51
阅读 3852·2021-11-22 15:29
阅读 3314·2021-10-08 10:05
阅读 1667·2021-09-22 15:20
阅读 1156·2019-08-30 15:56
阅读 1153·2019-08-30 15:54
阅读 805·2019-08-26 11:54
阅读 2702·2019-08-26 11:32