session.init.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 - 工具库-session
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class sessionInit {
  12. /**
  13. * Session-设置session值
  14. * 使用方法:$this->getUtil('session')->set('ccccccc', 'dddddd');
  15. * @param string $key key值,可以为单个key值,也可以为数组
  16. * @param string $value value值
  17. * @return string
  18. */
  19. public function set($key, $value='') {
  20. if (!session_id()) $this->start();
  21. if (!is_array($key)) {
  22. $_SESSION[$key] = $value;
  23. } else {
  24. foreach ($key as $k => $v) $_SESSION[$k] = $v;
  25. }
  26. return true;
  27. }
  28. /**
  29. * Session-获取session值
  30. * 使用方法:$this->getUtil('session')->get('ccccccc');
  31. * @param string $key key值
  32. * @return string
  33. */
  34. public function get($key) {
  35. if (!session_id()) $this->start();
  36. return (isset($_SESSION[$key])) ? $_SESSION[$key] : NULL;
  37. }
  38. /**
  39. * Session-删除session值
  40. * 使用方法:$this->getUtil('session')->del('ccccccc');
  41. * @param string $key key值
  42. * @return string
  43. */
  44. public function del($key) {
  45. if (!session_id()) $this->start();
  46. if (is_array($key)) {
  47. foreach ($key as $k){
  48. if (isset($_SESSION[$k])) unset($_SESSION[$k]);
  49. }
  50. } else {
  51. if (isset($_SESSION[$key])) unset($_SESSION[$key]);
  52. }
  53. return true;
  54. }
  55. /**
  56. * Session-清空session
  57. * 使用方法:$this->getUtil('session')->clear();
  58. * @return
  59. */
  60. public function clear() {
  61. if (!session_id()) $this->start();
  62. session_destroy();
  63. $_SESSION = array();
  64. }
  65. /**
  66. * Session-session_start()
  67. * @return string
  68. */
  69. private function start() {
  70. session_start();
  71. }
  72. }