- <?php
- /**
- * 用于rpc服务端的header系列函数
- * @author flynetcn
- */
- class Utils_Header
- {
- private static $headers = array();
- private static $cookies = array();
- private static $sessionOpened = false;
- public static function header($string, $replace=true, $http_response_code=null)
- {
- if (PHP_SAPI == 'cli') {
- if ($http_response_code) {
- self::$headers[] = array($string, $replace, $http_response_code);
- } else {
- self::$headers[] = array($string, $replace);
- }
- }
- if ($http_response_code) {
- header($string, $replace, $http_response_code);
- } else {
- header($string, $replace);
- }
- }
- public static function session_start()
- {
- if (PHP_SAPI == 'cli' && !self::$sessionOpened) {
- //模拟session机制
- if (!$_COOKIE[session_name()]) {
- $ip_prefix = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? ip2long($_SERVER['HTTP_X_FORWARDED_FOR']) : rand(1, 10000);
- $pid_prefix = posix_getpid();
- $sessid = uniqid(strval(dechex($ip_prefix)).strval(dechex($pid_prefix)));
- $_COOKIE[session_name()] = $sessid;
- session_start();
- self::$cookies[] = array(session_name(), $sessid, 0, '/', DOMAIN_COOKIE);
- } else {
- session_start();
- }
- self::$sessionOpened = true;
- return true;
- } else {
- return session_start();
- }
- }
- public static function session_write_close()
- {
- if (self::$sessionOpened) {
- self::$sessionOpened = false;
- }
- $_SESSION = array();
- session_write_close();
- }
- public static function setcookie($name, $value='', $expire=0, $path='', $domain='', $secure=false, $httponly=false)
- {
- if (PHP_SAPI == 'cli') {
- self::$cookies[] = func_get_args();
- }
- return call_user_func_array('setcookie', func_get_args());
- }
- public static function getSendCookies()
- {
- return self::$cookies;
- }
- public static function getSendHeaders()
- {
- return self::$headers;
- }
- public static function cleanSendCookies()
- {
- self::$cookies = array();
- }
- public static function cleanSendHeaders()
- {
- self::$headers = array();
- }
- }
12-25 01:55