PHP 输出控制

Jun 23, 2016 pm 02:31 PM

  以apache2 的mod_php5模块为例,通过php_ap2_register_hook()函数来注册钩子,pre_config,post_config,child_init是启动挂钩,它们在服务器启动时调用,handler 是请求钩子,挂钩到apache 的一次请求。很自然的,要在apache启动和请求的时候,分别完成不同的工作。  

void php_ap2_register_hook(apr_pool_t *p){	ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);	ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);	ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);	ap_hook_child_init(php_apache_child_init, NULL, NULL, APR_HOOK_MIDDLE);}
Copy after login

 在php_apache_server_startup中

static intphp_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){	void *data = NULL;	const char *userdata_key = "apache2hook_post_config";	/* Apache will load, unload and then reload a DSO module. This	 * prevents us from starting PHP until the second load. */	apr_pool_userdata_get(&data, userdata_key, s->process->pool);	if (data == NULL) {		/* We must use set() here and *not* setn(), otherwise the		 * static string pointed to by userdata_key will be mapped		 * to a different location when the DSO is reloaded and the		 * pointers won't match, causing get() to return NULL when		 * we expected it to return non-NULL. */		apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);		return OK;	}	/* Set up our overridden path. */	if (apache2_php_ini_path_override) {		apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override;	}#ifdef ZTS	tsrm_startup(1, 1, 0, NULL);#endif	sapi_startup(&apache2_sapi_module);//完成sapi的启动,初始化全局变量等	apache2_sapi_module.startup(&apache2_sapi_module);//通过调用_sapi_module_struct的startup函数,注册apache sapi module 一系列函数	apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);	php_apache_add_version(pconf);	return OK;}
Copy after login

  随后又会调用php_module_startup来启动作为apache一个模块的PHP,初始化一些PHP的状态,函数等等。这篇文章要用到的代码为:

int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules){	zend_utility_functions zuf;。。。//省略部分代码       php_output_startup();//初始化PHP的输出函数	zuf.error_function = php_error_cb;	zuf.printf_function = php_printf;	zuf.write_function = php_body_write_wrapper;//PHP的默认输出函数	zuf.fopen_function = php_fopen_wrapper_for_zend;	zuf.message_handler = php_message_handler_for_zend;	zuf.block_interruptions = sapi_module.block_interruptions;	zuf.unblock_interruptions = sapi_module.unblock_interruptions;	zuf.get_configuration_directive = php_get_configuration_directive_for_zend;	zuf.ticks_function = php_run_ticks;	zuf.on_timeout = php_on_timeout;	zuf.stream_open_function = php_stream_open_for_zend;	zuf.vspprintf_function = vspprintf;	zuf.getenv_function = sapi_getenv;	zuf.resolve_path_function = php_resolve_path_for_zend;	zend_startup(&zuf, NULL TSRMLS_CC);。。。//省略部分代码}
Copy after login

  PHPAPI void php_output_startup(void)->static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC)

此时PHP的默认一系列输出函数为:  

static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC){	OG(php_body_write) = php_default_output_func;	OG(php_header_write) = php_default_output_func;	OG(implicit_flush) = 0;	OG(output_start_filename) = NULL;	OG(output_start_lineno) = 0;}
Copy after login

 

/* {{{ php_default_output_func */PHPAPI int php_default_output_func(const char *str, uint str_len TSRMLS_DC){	fwrite(str, 1, str_len, stderr);//默认输出到标准错误/* See http://support.microsoft.com/kb/190351 */#ifdef PHP_WIN32	fflush(stderr);#endif	return str_len;}
Copy after login

  上述情况是发生在apache启动的时候,初始化PHP的过程,在每一次请求过程中,显然输出函数不对。跟踪函数php_handler->php_apache_request_ctor->php_request_startup-> php_output_activate,在这个函数中:

PHPAPI void php_output_activate(TSRMLS_D){	OG(php_body_write) = php_ub_body_write;//输出函数	OG(php_header_write) = sapi_module.ub_write;	OG(ob_nesting_level) = 0;	OG(ob_lock) = 0;	OG(disable_output) = 0;	OG(output_start_filename) = NULL;	OG(output_start_lineno) = 0;}PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC){	int result = 0;	if (SG(request_info).headers_only) {		if(SG(headers_sent)) {			return 0;		}		php_header(TSRMLS_C);		zend_bailout();	}	if (php_header(TSRMLS_C)) {		if (zend_is_compiling(TSRMLS_C)) {			OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C);			OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C);		} else if (zend_is_executing(TSRMLS_C)) {			OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C);			OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C);		}		OG(php_body_write) = php_ub_body_write_no_header;//输出函数重新赋值		result = php_ub_body_write_no_header(str, str_length TSRMLS_CC);	}	return result;}PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC){	int result;	if (OG(disable_output)) {		return 0;	}	result = OG(php_header_write)(str, str_length TSRMLS_CC);//实际上调用的是OG(php_header_write)也就是sapi_module.ub_write为apache的函数	if (OG(implicit_flush)) {		sapi_flush(TSRMLS_C);	}	return result;}
Copy after login

  这样一来结果就了然了,在PHP启动的时候,输出函数指向的fd是stderr,在每一次请求的时候,再将函数指针指向apache的输出函数。

  在zend_startup的时候会有:  

ZEND_API zend_write_func_t zend_write;zend_write = (zend_write_func_t) utility_functions->write_function;
Copy after login

  在实际使用的时候,以echo为例,最终会调用:

ZEND_API int zend_print_zval(zval *expr, int indent) /* {{{ */{	return zend_print_zval_ex(zend_write, expr, indent);//zend_write为实际调用的指针函数}
Copy after login

 想了一下PHP在请求的时候,可以动态的指定输出函数,这样更加方便SAPI层的接口编写,这个输出函数的控制权力在server端才合理,但是PHP在自身上面包装了很多层,不知道是不是命名和年代久远的关系。

   

  

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

See all articles