views:

205

answers:

6

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.

+2  A: 

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.

Don Werve
Don is correct. include, include_once, require, and require_once all function by executing the target file. PHP has no notion of "including without executing".
Kalium
+1  A: 

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.

Luc M
+1  A: 

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.

Brent Baisley
If what he's saying is accurate, he's loading a php file, and it does an include("whatever.h") command, so it doesn't matter what the apache setting is.
Kip
Reread the question, it's not clear but I think maybe you're right and the user actually is going to site.com/whatever.h
Kip
No, I'm going to site.com/index.html. The apache config is executing the .html file as PHP and running include_once("whatever.h"). I don't want to change all the .h files yet - there are a lot of them. A rewrite of the site is in the works.
jeffkolez
A: 

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

Kip
+6  A: 

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.

Kip
A: 

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.

Brent Baisley