简体   繁体   中英

Including different types of files in PHP

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.

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)?

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.

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.

I'm running the code on a Macbook with the latest versions available. PHP 5.2.6, postgresql 8.3 and 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.

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

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 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.

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

you can use a .htaccess file and add:

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.

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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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