LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Set up Apache to read php IN html code? (https://www.linuxquestions.org/questions/linux-networking-3/set-up-apache-to-read-php-in-html-code-138991/)

WorldBuilder 01-26-2004 06:22 PM

Set up Apache to read php IN html code?
 
Hi guys,

I know a reasonable amount about Apache, but am no expert. Have a question for you. My Apache server is running on linux. All of my pages on my site are .html extensions (except for a few .php pages. Recently, I started learning php. One thing I tried was to put php code INTO html. When I did this and tried to view the site, the php code would actually show up ON the site in the browser. I have been told that:
Quote:

"PHP has to be in pages that end with the .php extension, unless your server is set up to run .html pages through the parser. Most shared hosting is set up to only process php scripting in .php, .php3 and .php4 pages."
I'll look into it, but what exactly does that mean, and what do I have to do in httpd.conf to allow php code to run in html? Thanks!

Chris

leckie 01-27-2004 04:54 AM

gee i didn't think u had to do anything, unlike perl.
So does php work at all, do pages with .php extentions work.
if so inline php should work as well.
but u cannot just start coding u have to encapsulate it eg
<?php echo "Hello World<P>"; ?>

GraemeK 01-27-2004 05:46 AM

what does

<?
phpinfo();
?>

show? i.e create a html file and put the above code in it. then name the file index.php and stick it in your apache root.

you should see tones of info about your php system.

if not php aint working!

WorldBuilder 01-27-2004 07:43 AM

Thanks for the replies, guys. I DO see pages ending in .php, and I know php is installed and working correctly. What's NOT working is when I have an html page with php code IN the html code. When that happens, the php code is displayed on the page itself. The only advice I've been able to find is what I quoted in my original post.

What I mean is this:
Quote:

show? i.e create a html file and put the above code in it. then name the file index.php and stick it in your apache root.
I have that already. The php install automatically generated it. BUT, if I create a HTML extension file and place all that php code into it, then that WON'T work. The files are identical, except one ends in .php and one ends in .html. The .php works, the .html (with php code IN it) doesn't.

Chris

leckie 01-27-2004 07:50 AM

can u put
<body>
<?php echo "Hello World<P>"; ?>
</body>
into a file called test.html

what does it do

WorldBuilder 01-27-2004 08:52 AM

Hello again.

If I do that, it shows all the code in the browser itself. I attached a screenshot for you to see. If I then change the filename to "test.php", though, it displays only "Hello World" as it should.

http://www.bartlett-family.net/quiz/image1.jpg

The file itself is here. The same file (but with a php extension) is here.

Chris

WorldBuilder 01-27-2004 09:01 AM

In addition, I've been reading up on this. I already added:
Code:

AddType application/x-httpd-php .php .html
to my httpd.conf file. I believe that this should allow php code INSIDE html files. I have also read that I have to add "ScriptAlias" something or other to the httpd.conf file. For instance, I had to add:
Code:

ScriptAlias /cgi-bin/ "/blah/blah/blah/cgi-bin/"
to allow for cgi files. The problem with that is that php files are not all in one directory as cgi files are! How can I add this ScriptAlias line to the httpd.conf with more than one directory accessible? Should I just make the directory my DocumentRoot directory with a trailing "/"?

Chris

GraemeK 01-27-2004 09:38 AM

You could have a html file ending in .php and if no php code was in it then the file would be ouputed as is. if the html file had <?php ?> then what ever is in those brackets will be passed to the php pasher where the code would be interupted.

WorldBuilder 01-27-2004 09:53 AM

Quote:

Originally posted by GraemeK
You could have a html file ending in .php and if no php code was in it then the file would be ouputed as is. if the html file had <?php ?> then what ever is in those brackets will be passed to the php pasher where the code would be interupted.
Huh? Sorry, I don't understand what you're saying. All I want to do is allow php code inside .html files. There seems to be some setting in Apache's httpd.conf file I have to change, but I don't know what it is. Something to do with a parser...?

GraemeK 01-27-2004 11:25 AM

as far as i know any html file that contains php data has to end in .php in order to be pased to the php parser and outputed.

getting php code outputed correctly and still retaining the .html extension isnt possible i didnt think.

WorldBuilder 01-27-2004 12:16 PM

Quote:

Originally posted by GraemeK
as far as i know any html file that contains php data has to end in .php in order to be pased to the php parser and outputed.

getting php code outputed correctly and still retaining the .html extension isnt possible i didnt think.

It is possible. Actually, I just figured it out! ;) You have to add the above-mentioned lines to httpd.conf as well as:
Code:

AddHandler php-script .php .html
Now, you can add php code to html files and have it execute as if it were a regular php file. :D Cool!

Chris


All times are GMT -5. The time now is 05:10 PM.