在PHP中包含不同类型的文件

时间:2022-10-05 21:17:32

I took over a PHP project and I'm trying to get it running on my dev box. I'm a developer and not a sysadmin, so I'm having trouble getting it working.

我接手了一个PHP项目,我试图让它在我的开发盒上运行。我是开发人员,而不是系统管理员,所以我无法让它工作。

The previous developer used .h files to include PHP. My current configuration is taking that .h file and including it without executing it. What do I need to look for in my apache config (or is it my php.ini)?

以前的开发人员使用.h文件来包含PHP。我当前的配置是采用该.h文件并包含它而不执行它。在我的apache配置中需要查找什么(或者是我的php.ini)?

EDIT:

Something clicked when I read one of the comments below. The code is using ASP style tags "<?". I've turned the option on in php.ini and according to phpinfo(), it's enabled, but apache is still just including the code as text.

当我阅读下面的评论之一时点击了一些东西。代码使用ASP样式标签“

I just checked it and running the code with the full opening PHP tag "<?php" fixes the problem. Having said that, I'd still like it to work the other way.

我只是检查它并运行代码与完整的开放PHP标签“

I'm running the code on a Macbook with the latest versions available. PHP 5.2.6, postgresql 8.3 and apache 2.

我正在使用最新版本的Macbook上运行代码。 PHP 5.2.6,postgresql 8.3和apache 2。

The code works on the staging server, but I can't figure out what the difference it.

代码适用于登台服务器,但我无法弄清楚它有什么区别。

EDIT

Durrr...I didn't have short_open_tags enabled in php.ini.

Durrr ...我没有在php.ini中启用short_open_tags。

6 个解决方案

#1


Could the problem be that the .h file you are including just starts into php code without the opening "<?php" tag?

可能问题是你所包含的.h文件只是在没有打开“

From include documentation:

来自包含文档:

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

当包含文件时,解析将脱离PHP模式并在目标文件的开头进入HTML模式,并在结束时再次恢复。因此,目标文件中应作为PHP代码执行的任何代码都必须包含在有效的PHP开始和结束标记中。

#2


PHP is an interpreted language, so you can't 'include' a file without executing it. Remember that '.h' is just a file extension, so although the previous coder may have put PHP in a '.h' file, it's still just PHP.

PHP是一种解释型语言,因此您无法在不执行文件的情况下“包含”该文件。请记住'.h'只是一个文件扩展名,所以虽然之前的编码器可能已将PHP放在'.h'文件中,但它仍然只是PHP。

More details on the problems you're running into would be helpful.

有关您遇到的问题的更多详细信息将会有所帮助。

#3


you can use a .htaccess file and add:

你可以使用.htaccess文件并添加:

php_value auto_prepend_file /path/to/include-file.h

php_value auto_prepend_file /path/to/include-file.h


Edit

I've just read that .htaccess only works with the module version of php.

我刚才读到.htaccess只适用于php的模块版本。

#4


You should change them all to .php extensions. But, if you are going to leave them as .h, you change the mapping in Apache. It's Apache that runs the file through the proper interpreter, not PHP. The PHP engine will process anything passed to it.

您应该将它们全部更改为.php扩展名。但是,如果您要将它们保留为.h,则可以更改Apache中的映射。它是Apache通过正确的解释器运行文件,而不是PHP。 PHP引擎将处理传递给它的任何内容。

#5


include and require will execute the included file. If you are using readfile, it simply echoes the file without treating it as PHP code.

include和require将执行包含的文件。如果您使用的是readfile,它只是回显文件而不将其视为PHP代码。

#6


If the .h files are "missing" the <?php directive, then you may have to switch from include to something like:

如果.h文件“缺少”

eval( file_get_contents("file.h") );

Personally I try to avoid eval whenever I can, but sometimes there is just no choice.

我个人尽量避免使用eval,但有时候别无选择。

#1


Could the problem be that the .h file you are including just starts into php code without the opening "<?php" tag?

可能问题是你所包含的.h文件只是在没有打开“

From include documentation:

来自包含文档:

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

当包含文件时,解析将脱离PHP模式并在目标文件的开头进入HTML模式,并在结束时再次恢复。因此,目标文件中应作为PHP代码执行的任何代码都必须包含在有效的PHP开始和结束标记中。

#2


PHP is an interpreted language, so you can't 'include' a file without executing it. Remember that '.h' is just a file extension, so although the previous coder may have put PHP in a '.h' file, it's still just PHP.

PHP是一种解释型语言,因此您无法在不执行文件的情况下“包含”该文件。请记住'.h'只是一个文件扩展名,所以虽然之前的编码器可能已将PHP放在'.h'文件中,但它仍然只是PHP。

More details on the problems you're running into would be helpful.

有关您遇到的问题的更多详细信息将会有所帮助。

#3


you can use a .htaccess file and add:

你可以使用.htaccess文件并添加:

php_value auto_prepend_file /path/to/include-file.h

php_value auto_prepend_file /path/to/include-file.h


Edit

I've just read that .htaccess only works with the module version of php.

我刚才读到.htaccess只适用于php的模块版本。

#4


You should change them all to .php extensions. But, if you are going to leave them as .h, you change the mapping in Apache. It's Apache that runs the file through the proper interpreter, not PHP. The PHP engine will process anything passed to it.

您应该将它们全部更改为.php扩展名。但是,如果您要将它们保留为.h,则可以更改Apache中的映射。它是Apache通过正确的解释器运行文件,而不是PHP。 PHP引擎将处理传递给它的任何内容。

#5


include and require will execute the included file. If you are using readfile, it simply echoes the file without treating it as PHP code.

include和require将执行包含的文件。如果您使用的是readfile,它只是回显文件而不将其视为PHP代码。

#6


If the .h files are "missing" the <?php directive, then you may have to switch from include to something like:

如果.h文件“缺少”

eval( file_get_contents("file.h") );

Personally I try to avoid eval whenever I can, but sometimes there is just no choice.

我个人尽量避免使用eval,但有时候别无选择。