Home  >  Article  >  Backend Development  >  [thrift] Header series functions for php server

[thrift] Header series functions for php server

WBOY
WBOYOriginal
2016-07-30 13:29:471074browse
<?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_resp
	{
		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, $http
	{
		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();
	}
}

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the [thrift] header series functions for the PHP server, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn