简体   繁体   English

在PHP中输出标题之前调用函数?

[英]Call a function before outputting headers in PHP?

Is there any way to have PHP automatically call a function, before a script outputs any HTTP headers? 在脚本输出任何HTTP头之前,有没有办法让PHP自动调用函数?

I'm looking for something like register-shutdown-function , but to register a function that's called before the output is already sent, not after. 我正在寻找像register-shutdown-function这样的东西,但要注册一个在输出已经发送之前调用的函数,而不是之后。 I want my function to send a header, so I need something that's called earlier. 我希望我的函数发送一个标题,所以我需要一些之前调用过的东西。

You could also trap everything with ob_start and then register a callback function to be used when you send the page with ob_end_flush . 您还可以使用ob_start捕获所有内容,然后注册一个回调函数,以便在使用ob_end_flush发送页面时使用。 Check out the PHP manual for OB_START 查看OB_START的PHP手册

I don't know if it is what you are looking for but you might want to investigate using auto_prepend_file in your php.ini or setting it in an .htaccess file. 我不知道你是否正在寻找它,但你可能想在php.ini中使用auto_prepend_file或在.htaccess文件中设置它。 If you set an auto_prepend_file it will automatically include that file before running each script. 如果设置auto_prepend_file,它将在运行每个脚本之前自动包含该文件。

auto_prepend_file 的auto_prepend_file

查看http://www.webmasterworld.com/forum88/1225.htm您将使用PHP的header()函数发送标题(必须在任何其他输出之前调用),但在使用之前/期间使用您的函数呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM