资讯专栏INFORMATION COLUMN

PHP 常用类分享(记录)

seal_de / 1377人阅读

摘要:加密解密类由于开始废弃传统的加密函数而改用,因此该类支持的版本为简易加解密类通用秘钥所有可用的加密类型可参考构造函数加密类型。

加密解密类

由于 PHP7.1 开始废弃传统的加密函数 mcrypt_encrypt 而改用 openssl_encrypt,因此该类支持的 PHP 版本为: (PHP 5 >= 5.3.0, PHP 7)

</>复制代码

  1. METHOD = $method;
  2. }
  3. }
  4. /**
  5. * 加密字符串
  6. *
  7. * @param String 待加密数据
  8. * @param string 加密秘钥,若为空,则使用通用秘钥
  9. * @return void
  10. */
  11. public function encrypt($input, $key = "") {
  12. if (empty($key)) $key = $this->KEY;
  13. $data = openssl_encrypt($input, $this->METHOD, $key, OPENSSL_RAW_DATA);
  14. $data = base64_encode($data);
  15. return $data;
  16. }
  17. /**
  18. * 解密字符串
  19. *
  20. * @param String 待解密字符串
  21. * @param string 解密秘钥,若为空,则使用通用秘钥
  22. * @return void
  23. */
  24. public function decrypt($input, $key = "") {
  25. if (empty($key)) $key = $this->KEY;
  26. $data = openssl_decrypt(base64_decode($input), $this->METHOD, $key, OPENSSL_RAW_DATA);
  27. return $data;
  28. }
  29. }
发送邮件类

先下载 PHPMailer,解压后,将其放置你的项目中,并修改类中的引入路径。

</>复制代码

  1. _init();
  2. $this->_baseURL = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"];
  3. }
  4. private function _init() {
  5. if ($this->_email == null) {
  6. $mail = new PHPMailer(true);
  7. try {
  8. $mail->SMTPDebug = 0;
  9. $mail->isSMTP();
  10. $mail->Host = get_global_config("mail.Host");
  11. $mail->SMTPAuth = true;
  12. $mail->Username = get_global_config("mail.Username");
  13. $mail->Password = get_global_config("mail.Password");
  14. $mail->SMTPSecure = get_global_config("mail.SMTPSecure");
  15. $mail->Port = get_global_config("mail.Port");
  16. $mail->setFrom(get_global_config("mail.Username"), get_global_config("mail.Fromname"));
  17. $this->_email = $mail;
  18. return true;
  19. } catch (Exception $e) {
  20. // return null;
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. public function sendEmail($toEmail, $toUsername, $subject, $body) {
  27. if (!$this->_email) return false;
  28. try {
  29. $this->_email->addAddress($toEmail, $toUsername);
  30. $this->_email->isHTML(true);
  31. $this->_email->Subject = $subject;
  32. $this->_email->Body = $body;
  33. // 当邮件不支持 HTML 时显示的邮件内容
  34. $this->_email->AltBody = $body;
  35. $this->_email->send();
  36. // echo "Message has been sent";
  37. return true;
  38. } catch (Exception $e) {
  39. return false;
  40. // echo "Message could not be sent.";
  41. // echo "Mailer Error: " . $this->_email->ErrorInfo;
  42. }
  43. }
  44. }
模拟锁

使用 Redis 模拟进程锁,参考至 discuz 的进程锁源码。

</>复制代码

  1. cache = Roc::redis();
  2. $this->cache->select(Roc::get("redis.db"));
  3. }
  4. /**
  5. * 是否被锁
  6. *
  7. * @param string 锁名
  8. * @param integer 上锁时间
  9. * @return boolean
  10. */
  11. public function islocked($process, $ttl = 0) {
  12. $ttl = $ttl < 1 ? 600 : intval($ttl);
  13. return $this->_status("get", $process) || $this->_find($process, $ttl);
  14. }
  15. /**
  16. * 解锁
  17. *
  18. * @param string 锁名
  19. * @return void
  20. */
  21. public function unlock($process) {
  22. $this->_status("rm", $process);
  23. $this->_cmd("rm", $process);
  24. }
  25. private function _status($action, $process) {
  26. static $plist = array();
  27. switch ($action) {
  28. case "set" : $plist[$process] = true; break;
  29. case "get" : return !empty($plist[$process]); break;
  30. case "rm" : $plist[$process] = null; break;
  31. case "clear" : $plist = array(); break;
  32. }
  33. return true;
  34. }
  35. private function _find($name, $ttl) {
  36. if(!$this->_cmd("get", $name)) {
  37. $this->_cmd("set", $name, $ttl);
  38. $ret = false;
  39. } else {
  40. $ret = true;
  41. }
  42. // $this->_status("set", $name);
  43. return $ret;
  44. }
  45. private function _cmd($cmd, $name, $ttl = 0) {
  46. return $this->_process_cmd_memory($cmd, $name, $ttl);
  47. // static $allowmem;
  48. // if($allowmem === null) {
  49. // $allowmem = Roc::get("redis.db");
  50. // }
  51. // if($allowmem) {
  52. // return self::_process_cmd_memory($cmd, $name, $ttl);
  53. // } else {
  54. // return false;
  55. // // return self::_process_cmd_db($cmd, $name, $ttl);
  56. // }
  57. }
  58. private function _process_cmd_memory($cmd, $name, $ttl = 0) {
  59. $ret = "";
  60. $name = "process_lock_".$name;
  61. switch ($cmd) {
  62. case "set" :
  63. $ret = $this->cache->setex($name, time(), $ttl);
  64. // $ret = memory("set", "process_lock_".$name, time(), $ttl);
  65. break;
  66. case "get" :
  67. $ret = $this->cache->get($name);
  68. // $ret = memory("get", "process_lock_".$name);
  69. break;
  70. case "rm" :
  71. $ret = $this->cache->delete($name);
  72. // $ret = memory("rm", "process_lock_".$name);
  73. }
  74. echo $cmd . ":";
  75. var_dump($ret);
  76. return $ret;
  77. }
  78. private function _process_cmd_db($cmd, $name, $ttl = 0) {
  79. $ret = "";
  80. switch ($cmd) {
  81. case "set":
  82. $ret = C::t("common_process")->insert(array("processid" => $name, "expiry" => time() + $ttl), FALSE, true);
  83. break;
  84. case "get":
  85. $ret = C::t("common_process")->fetch($name);
  86. if(empty($ret) || $ret["expiry"] < time()) {
  87. $ret = false;
  88. } else {
  89. $ret = true;
  90. }
  91. break;
  92. case "rm":
  93. $ret = C::t("common_process")->delete_process($name, time());
  94. break;
  95. }
  96. return $ret;
  97. }
  98. }
Redis

一个简易的 Redis 操作类,支持在查找不到缓存时,进行相对应的回调操作。

</>复制代码

  1. cache = new Redis();
  2. }
  3. // ============= String 操作
  4. /**
  5. * 设置缓存
  6. *
  7. * @param String 缓存key值
  8. * @param String/Object 缓存数据,可为字符串可为数组。
  9. * @param Int 过期时间
  10. * @return Boolean
  11. */
  12. public function set($key, $value, $ttl = null) {
  13. if (is_array($value)) $value = serialize($value);
  14. if ($ttl !== null && $ttl > 0)
  15. return $this->cache->setex($this->_key($key), $ttl, $value);
  16. else
  17. return $this->cache->set($this->_key($key), $value);
  18. }
  19. /**
  20. * 获取缓存数据
  21. *
  22. * @param String 值
  23. * @param Data 默认值,设置后,如果缓存没有数据则返回默认值
  24. * @return Data
  25. */
  26. public function get($key, $callback = false) {
  27. $res = $this->cache->get($this->_key($key));
  28. if ($res === false && $callback === true) {
  29. $method_name = "_$key";
  30. if (method_exists($this, $method_name)) {
  31. return $this->$method_name();
  32. } else {
  33. return false;
  34. }
  35. }
  36. // 如果反序列化成功,则返回反序列化的数据。
  37. // @防止报 warning。
  38. if (@unserialize($res) !== false) return unserialize($res);
  39. return $res;
  40. }
  41. /**
  42. * 删除缓存
  43. *
  44. * @param String 需要删除的键名
  45. * @return true or false
  46. */
  47. public function rm($key) {
  48. return $this->cache->delete($this->_key($key));
  49. }
  50. public function inc($key, $step = 1) {
  51. return $this->cache->incr($this->_key($key), $step);
  52. }
  53. public function dec($key, $step = 1) {
  54. return $this->cache->decr($this->_key($key), $step);
  55. }
  56. private function _key($key) {
  57. return $this->prefix . $key;
  58. }
  59. public function getMultiple($keys) {
  60. $_keys = [];
  61. foreach ($keys as $key) $_keys[] = $this->_key($key);
  62. return $this->cache->getMultiple($_keys);
  63. }
  64. // ============== Hash 操作
  65. public function hset($hash, $key, $value) {
  66. return $this->cache->hSet($hash, $key, $value);
  67. }
  68. public function hget($hash, $key) {
  69. return $this->cache->hGet($hash, $key);
  70. }
  71. public function hgetall($hash) {
  72. return $this->cache->hGetAll($hash);
  73. }
  74. public function hvals($hash) {
  75. return $this->cache->hVals($hash);
  76. }
  77. public function hkeys($hash) {
  78. return $this->cache->hKeys($hash);
  79. }
  80. // ================ 通用回调
  81. // Your callback code here.
  82. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/26208.html

相关文章

  • Laravel核心解读--异常处理

    摘要:请求未通过的验证时会抛出此异常。异常处理是非常重要但又容易让开发者忽略的功能,这篇文章简单解释了内部异常处理的机制以及扩展异常处理的方式方法。 异常处理是编程中十分重要但也最容易被人忽视的语言特性,它为开发者提供了处理程序运行时错误的机制,对于程序设计来说正确的异常处理能够防止泄露程序自身细节给用户,给开发者提供完整的错误回溯堆栈,同时也能提高程序的健壮性。 这篇文章我们来简单梳理一下...

    includecmath 评论0 收藏0
  • PHP小知识点

    摘要:那些琐碎的知识点作者记录的的很奇特很难记的知识点。易错知识点整理注意和的区别中和都是输出的作用,但是两者之间还是有细微的差别。今天手头不忙,总结一下,分享过程中掌握的知识点。 深入理解 PHP 之:Nginx 与 FPM 的工作机制 这篇文章从 Nginx 与 FPM 的工作机制出发,探讨配置背后的原理,让我们真正理解 Nginx 与 PHP 是如何协同工作的。 PHP 那些琐碎的知识...

    hover_lew 评论0 收藏0
  • PHP后端组织项目结构的思考

    摘要:介绍下一个新项目,后端该如何从零去搭建。我们先假设这个项目由两部组成提供给站点使用的提供给运营人员使用的管理后台。因此通过回顾,我们得出我们的后端项目需要一个的层次,来存放业务逻辑。 这是 后端开发者从零做一个移动应用 的后端部分第二篇。介绍下一个新项目,后端该如何从零去搭建。我们先假设这个项目由两部组成 提供给wap站点、app使用的api; 提供给运营人员使用的管理后台。 整个...

    cikenerd 评论0 收藏0
  • PHP笔试面试题精选(一)

    摘要:,跨站脚本攻击。实际发的请求就是,用于表示这是一个请求。,用于告知服务器根据这个参数获取回调函数的名称,通常约定就叫。,回调函数的名称,也是前面参数的值,可省略,会自动生成。 本次课程主要围绕 PHP 面试和笔试中经常会出现的一些知识点,但是面试官会在笔试题基础上深入扩展,那么你知道如何更好的回答让面试官满意吗?题目收集自腾讯,迅雷,美图等公司的笔试面试题,以及本人面试经历中印象中的知...

    JohnLui 评论0 收藏0
  • SegmentFault 技术周刊 Vol.40 - 2018,来学习一门新的编程语言吧!

    摘要:入门,第一个这是一门很新的语言,年前后正式公布,算起来是比较年轻的编程语言了,更重要的是它是面向程序员的函数式编程语言,它的代码运行在之上。它通过编辑类工具,带来了先进的编辑体验,增强了语言服务。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不觉已经到来了,总结过去的 2017,相信小伙们一定有很多收获...

    caspar 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<