Connecting Tech Pros Worldwide Forums | Help | Site Map

Noob - php works as .php but not when part of html

dogu
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm missing something and need a push in the right direction.
If I create a php file with the following content, and call it via a
browser, it works as expected.
<?php
echo "hello world";
?>

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>

I'm running
Mandrake 10.0
Apache 2
PHP 4
The php_mod is loaded for Apache.
I did the install for both Apache and PHP via Mandrakes built in rpm
management system.

TIA for any tips.

Doug

Geoff Berrow
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Noob - php works as .php but not when part of html


I noticed that Message-ID:
<zqOdnaUmufZv0u_cRVn-uA@metrocastcablevision.com> from dogu contained
the following:
[color=blue]
>I'm missing something and need a push in the right direction.
>If I create a php file with the following content, and call it via a
>browser, it works as expected.
><?php
>echo "hello world";
>?>
>
>If I embed the same thing in a simple HTML file, I get nothing.
><html>
><body>
><?php>
>echo "Hello world";
>?>
></body>
></html>[/color]

Unless you have configured Apache otherwise, the file must end in .php

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Geoff Berrow
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Noob - php works as .php but not when part of html


I noticed that Message-ID: <r8k4n05dtp2vmtofcqcnht8tbgbd0s6k3a@4ax.com>
from Geoff Berrow contained the following:
[color=blue][color=green]
>>If I embed the same thing in a simple HTML file, I get nothing.
>><html>
>><body>
>><?php>
>>echo "Hello world";
>>?>
>></body>
>></html>[/color]
>
>Unless you have configured Apache otherwise, the file must end in .php[/color]

Oops. Just noticed an erroneous '>'

try

<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
dogu
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Noob - php works as .php but not when part of html


Geoff Berrow wrote:[color=blue]
> I noticed that Message-ID: <r8k4n05dtp2vmtofcqcnht8tbgbd0s6k3a@4ax.com>
> from Geoff Berrow contained the following:
>
>[color=green][color=darkred]
>>>If I embed the same thing in a simple HTML file, I get nothing.
>>><html>
>>><body>
>>><?php>
>>>echo "Hello world";
>>>?>
>>></body>
>>></html>[/color]
>>
>>Unless you have configured Apache otherwise, the file must end in .php[/color]
>
>
> Oops. Just noticed an erroneous '>'
>
> try
>
> <html>
> <body>
> <?php
> echo "Hello world";
> ?>
> </body>
> </html>
>[/color]
Thanks, but what I posted was a type - in my actual code, the <?php line
is identical to yours.

What do you mean 'unless you have configured Apache otherwise...'? I
bet that's the problem but don't know what to configure.

Any tips would be appreciated.

Doug
Brad Kent
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Noob - php works as .php but not when part of html


Even with HTML intermingled amongst the PHP code, it's still a .php
file.
rename it whatever.php.

Of course you could tell apache to parse .html files as .php, but that
would be very bad advice.

dogu <dfinnerathome@netscape.net> wrote in message news:<zqOdnaUmufZv0u_cRVn-uA@metrocastcablevision.com>...[color=blue]
> I'm missing something and need a push in the right direction.
> If I create a php file with the following content, and call it via a
> browser, it works as expected.
> <?php
> echo "hello world";
> ?>
>
> If I embed the same thing in a simple HTML file, I get nothing.
> <html>
> <body>
> <?php>
> echo "Hello world";
> ?>
> </body>
> </html>
>
> I'm running
> Mandrake 10.0
> Apache 2
> PHP 4
> The php_mod is loaded for Apache.
> I did the install for both Apache and PHP via Mandrakes built in rpm
> management system.
>
> TIA for any tips.
>
> Doug[/color]
dogu
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Noob - php works as .php but not when part of html


Geoff Berrow wrote:[color=blue]
> I noticed that Message-ID: <r8k4n05dtp2vmtofcqcnht8tbgbd0s6k3a@4ax.com>
> from Geoff Berrow contained the following:
>
>[color=green][color=darkred]
>>>If I embed the same thing in a simple HTML file, I get nothing.
>>><html>
>>><body>
>>><?php>
>>>echo "Hello world";
>>>?>
>>></body>
>>></html>[/color]
>>
>>Unless you have configured Apache otherwise, the file must end in .php[/color]
>
>
> Oops. Just noticed an erroneous '>'
>
> try
>
> <html>
> <body>
> <?php
> echo "Hello world";
> ?>
> </body>
> </html>
>[/color]
Seems my confusion was thinking you could co-mingle php and html code in
files with html extensions. If I change the extension to .php it works
like a charm!

Take care.

Doug
Lüpher Cypher
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Noob - php works as .php but not when part of html


dogu wrote:[color=blue]
> Geoff Berrow wrote:
>[color=green]
>> I noticed that Message-ID: <r8k4n05dtp2vmtofcqcnht8tbgbd0s6k3a@4ax.com>
>> from Geoff Berrow contained the following:
>>
>>[color=darkred]
>>>> If I embed the same thing in a simple HTML file, I get nothing.
>>>> <html>
>>>> <body>
>>>> <?php>
>>>> echo "Hello world";
>>>> ?>
>>>> </body>
>>>> </html>
>>>
>>>
>>> Unless you have configured Apache otherwise, the file must end in .php[/color]
>>
>>
>>
>> Oops. Just noticed an erroneous '>'
>>
>> try
>>
>> <html>
>> <body>
>> <?php
>> echo "Hello world";
>> ?>
>> </body>
>> </html>
>>[/color]
> Seems my confusion was thinking you could co-mingle php and html code in
> files with html extensions. If I change the extension to .php it works
> like a charm!
>
> Take care.
>
> Doug[/color]

Bet you can set up the server to treat html files as php :)
Alvaro G. Vicario
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Noob - php works as .php but not when part of html


*** dogu escribió/wrote (Sun, 17 Oct 2004 10:32:42 -0400):[color=blue]
> Seems my confusion was thinking you could co-mingle php and html code in
> files with html extensions. If I change the extension to .php it works
> like a charm![/color]

There's no point in feeding *all* pages to the PHP interpreter. The *.php
extension is the way to parse only pages that contain code.


--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Data Goob
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Noob - php works as .php but not when part of html


Did you check your web server configuration?

In Apache on Linux, you might add:

AddType application/x-httpd-php .php .html

This will allow you to include PHP in your HTML as

<HTML>

blah blah blah...

<?php

php stuff blah blah blah

?>

</HTML>


You may have to have

AddOutputFilter INCLUDES .shtml
AddOutputFilter INCLUDES .html

added as well, but don't have the time to test. These turn on SSI's on
your pages, but I don't know if they affect embedded PHP or not. At
any rate I have both on my server and they work.

-DG-

dogu wrote:
[color=blue]
> Geoff Berrow wrote:
>[color=green]
>> I noticed that Message-ID: <r8k4n05dtp2vmtofcqcnht8tbgbd0s6k3a@4ax.com>
>> from Geoff Berrow contained the following:
>>
>>[color=darkred]
>>>> If I embed the same thing in a simple HTML file, I get nothing.
>>>> <html>
>>>> <body>
>>>> <?php>
>>>> echo "Hello world";
>>>> ?>
>>>> </body>
>>>> </html>
>>>
>>>
>>> Unless you have configured Apache otherwise, the file must end in .php[/color]
>>
>>
>>
>> Oops. Just noticed an erroneous '>'
>>
>> try
>>
>> <html>
>> <body>
>> <?php
>> echo "Hello world";
>> ?>
>> </body>
>> </html>
>>[/color]
> Seems my confusion was thinking you could co-mingle php and html code in
> files with html extensions. If I change the extension to .php it works
> like a charm!
>
> Take care.
>
> Doug[/color]

Michiel de Roo
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Noob - php works as .php but not when part of html


Data Goob wrote:[color=blue]
> Did you check your web server configuration?
>
> In Apache on Linux, you might add:
>
> AddType application/x-httpd-php .php .html[/color]

You probably want to put this in a .htaccess, so that it is NOT server-wide.
Data Goob
Guest
 
Posts: n/a
#11: Jul 17 '05

re: Noob - php works as .php but not when part of html


Right Roo, good point.

You could also do the Virtual host thang as well but I haven't tested, so I'm
unsure as to the rightness, truth, etc.

-DG-

Michiel de Roo wrote:[color=blue]
> Data Goob wrote:
>[color=green]
>> Did you check your web server configuration?
>>
>> In Apache on Linux, you might add:
>>
>> AddType application/x-httpd-php .php .html[/color]
>
>
> You probably want to put this in a .htaccess, so that it is NOT
> server-wide.[/color]

Geoff Berrow
Guest
 
Posts: n/a
#12: Jul 17 '05

re: Noob - php works as .php but not when part of html


I noticed that Message-ID:
<hamdnUs_3t8MHe_cRVn-rA@metrocastcablevision.com> from dogu contained
the following:
[color=blue]
>
>What do you mean 'unless you have configured Apache otherwise...'? I
>bet that's the problem but don't know what to configure.[/color]

What Brad said...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Data Goob
Guest
 
Posts: n/a
#13: Jul 17 '05

re: Noob - php works as .php but not when part of html


Well, now you've got my curiousity...

Why is it a bad idea to intermingle PHP with HTML?

Wasn't that the intent of PHP in the first place?

FWIW I'm not arguing one way or the other, just interested
in your opinion(s) .

-DG-

Geoff Berrow wrote:
[color=blue]
> I noticed that Message-ID:
> <hamdnUs_3t8MHe_cRVn-rA@metrocastcablevision.com> from dogu contained
> the following:
>
>[color=green]
>>What do you mean 'unless you have configured Apache otherwise...'? I
>>bet that's the problem but don't know what to configure.[/color]
>
>
> What Brad said...
>[/color]

Michael Vilain
Guest
 
Posts: n/a
#14: Jul 17 '05

re: Noob - php works as .php but not when part of html


In article <nWYcd.686$ix2.460@fe05.usenetserver.com>,
Data Goob <datagoob@hotmail.com> wrote:
[color=blue]
> Well, now you've got my curiousity...
>
> Why is it a bad idea to intermingle PHP with HTML?
>
> Wasn't that the intent of PHP in the first place?
>
> FWIW I'm not arguing one way or the other, just interested
> in your opinion(s) .
>
> -DG-
>
> Geoff Berrow wrote:
>[color=green]
> > I noticed that Message-ID:
> > <hamdnUs_3t8MHe_cRVn-rA@metrocastcablevision.com> from dogu contained
> > the following:
> >
> >[color=darkred]
> >>What do you mean 'unless you have configured Apache otherwise...'? I
> >>bet that's the problem but don't know what to configure.[/color]
> >
> >
> > What Brad said...
> >[/color][/color]

I've read many opinions on this. It all seems to revolve around the
scope of the project you're working on. Medium to large projects tend
to bog down with the code that generates the site and the actual HTML
itself. My own project is now to the point that I need to refactor it
to strip out embedded HTML and use a templating system instead, just to
make it easier to understand and support and grow.

I'm now seeing the result of forgetting stuff I coded months ago and
having to refamiliarize myself with it when I go back and update it.
I'm leaning toward a OOP page display class with templates.

In any case, as an old procedure coder who first learned FORTRAN in the
early 1970's and never used C very much (automatic variables really
confused me initially), it's been interesting having to rethink this
project.

Now I see why the database weenies like using the database as a
repository for the datastructures and business logic (i.e. Oracle's
triggers or MySQL's stored procedures in the next release) and the
application does the data capture, validation, and presentation.

--
DeeDee, don't press that button! DeeDee! NO! Dee...



Geoff Berrow
Guest
 
Posts: n/a
#15: Jul 17 '05

re: Noob - php works as .php but not when part of html


I noticed that Message-ID: <nWYcd.686$ix2.460@fe05.usenetserver.com>
from Data Goob contained the following:
[color=blue]
>Well, now you've got my curiousity...
>
>Why is it a bad idea to intermingle PHP with HTML?[/color]

I don't believe it is, necessarily.

But by default the resultant file has to be named <something>.php
whether it contains just raw php or a mixture of php and html.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Data Goob
Guest
 
Posts: n/a
#16: Jul 17 '05

re: Noob - php works as .php but not when part of html


Geoff Berrow wrote:
[color=blue]
> I noticed that Message-ID: <nWYcd.686$ix2.460@fe05.usenetserver.com>
> from Data Goob contained the following:
>
>[color=green]
>>Well, now you've got my curiousity...
>>
>>Why is it a bad idea to intermingle PHP with HTML?[/color]
>
>
> I don't believe it is, necessarily.
>
> But by default the resultant file has to be named <something>.php
> whether it contains just raw php or a mixture of php and html.
>[/color]
Actually this is not true. I have several HTML files with PHP
embedded. In fact they are not only with embedded PHP the PHP
statement is a PHP include, and I have SSI's as well... with the
..html suffix.


<HTML>
blah blah blah...

<?php include 'my-embedded-calendar.php' ?>

more HTML...

</HTML>

So what it really boils down to is in getting Apache to see .html
as something that can be interpreted as/with PHP. As a purist,
this might seem a violation and comingling of two different things,
but for me it is hybridization and the ability to component-ize
my sites for easier maintenance. Is it right for all web work?
Probably not appropriate in all situations, and may even create
problems with other add-ons like templates, etc. I view templates
as hybridization anyway, so for me the discussion is purely
academic. Template integration reduces portability, so if this is
the intent, don't do non-portable PHP/SSI/HTML/TEMPLATES/ETC.

In the final analysis, it works beautifully for my sites...


<HTML>

blah blah blah...

<!--include virtual="my-included-banner.html-->

<?php include 'my-embedded-calendar.php' ?>

more HTML...

</HTML>

Regards,

-DG-

Anonymous
Guest
 
Posts: n/a
#17: Jul 17 '05

re: Noob - php works as .php but not when part of html


Data Goob wrote:[color=blue]
>
> Well, now you've got my curiousity...
>
> Why is it a bad idea to intermingle PHP with HTML?[/color]

It isn't.

But when you do so you should name the file *.php, so that the server
knows it has to parse the file through php.

Alternativly you could tell the server to parse every file through php
but *that* is not a good idea. If all of your pages contain php it
wouldn't make any difference but if this is not the case the server goes
through quite a bit of unnecessary overhead.

That might not matter on your personal server that is only used for
testing, but no provider is going to configure his server like that
because less overhead means they can put more customers on one server.
So if you want your pages to run on any provider's server without having
to change them you should go with the usual file naming.

In other words: If it contains php name it *.php.

Bye!
Michael Fesser
Guest
 
Posts: n/a
#18: Jul 17 '05

re: Noob - php works as .php but not when part of html


.oO(Alvaro G. Vicario)
[color=blue]
>*** dogu escribió/wrote (Sun, 17 Oct 2004 10:32:42 -0400):[color=green]
>> Seems my confusion was thinking you could co-mingle php and html code in
>> files with html extensions. If I change the extension to .php it works
>> like a charm![/color]
>
>There's no point in feeding *all* pages to the PHP interpreter. The *.php
>extension is the way to parse only pages that contain code.[/color]

I consider it bad style to have both .html and .php appear in URLs.

On Apaches it's possible to parse some files with PHP and deliver others
directly, while keeping _all_ URLs consistent with the extension .html
(MultiViews).

Micha
John Dunlop
Guest
 
Posts: n/a
#19: Jul 17 '05

re: Noob - php works as .php but not when part of html


Michael Fesser wrote:
[color=blue]
> I consider it bad style to have both .html and .php appear in URLs.[/color]

Is there a reason for either?
[color=blue]
> On Apaches it's possible to parse some files with PHP and deliver others
> directly, while keeping _all_ URLs consistent with the extension .html[/color]

'.html' and other URI suffixes are redundant, contrary to
the principle of keeping URIs short and rememberable, and
deleterious to URI persistency. As a rule then it's best to
chop the wee nasties off.
[color=blue]
> (MultiViews).[/color]

Right.

Slainte! & HAGW!

--
Jock
Michael Fesser
Guest
 
Posts: n/a
#20: Jul 17 '05

re: Noob - php works as .php but not when part of html


.oO(John Dunlop)
[color=blue]
>Michael Fesser wrote:
>[color=green]
>> I consider it bad style to have both .html and .php appear in URLs.[/color]
>
>Is there a reason for either?[/color]

Not really. I use either .html or no extension at all.
[color=blue]
>'.html' and other URI suffixes are redundant, contrary to
>the principle of keeping URIs short and rememberable, and
>deleterious to URI persistency.[/color]

There's at least one point: Unexperienced users might expect web
addresses to start with 'www.' and end on '.html'. Sure we know that
both are not really necessary ...
[color=blue]
>As a rule then it's best to
>chop the wee nasties off.[/color]

That's what I will do in my next (testing) project, but for other
reasons. In short: With a URL like www.example.com/foo the server will
automatically decide according to the Accept header whether to send HTML
or XHTML back to the client. But I still want to be able to override the
content negotiation and force a particular content type if necessary, be
it for testing, for validation or simply for providing the same resource
in different formats (PDF for example). So to override the CN mechanismn
I would simply request /foo.html, /foo.xhtml or /foo.pdf if available.

That's just the very basic idea, I still have to think about some things
and implementation issues.

Micha
John Dunlop
Guest
 
Posts: n/a
#21: Jul 17 '05

re: Noob - php works as .php but not when part of html


Michael Fesser wrote:
[color=blue]
> There's at least one point: Unexperienced users might expect web
> addresses to start with 'www.' and end on '.html'. Sure we know that
> both are not really necessary ...[/color]

Fair enough. Although with the myriad different suffixes
out there today, '.php', '.asp', '.pl', '.cf' to name but a
few, I'm still unconvinced that surfers, inexperienced or
not, expect URIs to end in '.html'.

Also, I am seeing more and more URIs without suffixes on
television. Rightpondians can see the BBC's abbreviated
URIs, e.g. www.bbc.co.uk/weather -- that one's unfortunately
a redirection; hey, the beeb can't get it right all the
time, can they? If the big guns publish sensible URIs, the
practice will hopefully catch on soon.

[about chopping off URI suffixes]
[color=blue]
> That's what I will do in my next (testing) project, but for other
> reasons. In short: With a URL like www.example.com/foo the server will
> automatically decide according to the Accept header whether to send HTML
> or XHTML back to the client. But I still want to be able to override the
> content negotiation and force a particular content type if necessary, be
> it for testing, for validation or simply for providing the same resource
> in different formats (PDF for example). So to override the CN mechanismn
> I would simply request /foo.html, /foo.xhtml or /foo.pdf if available.[/color]

Sounds good to me.
[color=blue]
> That's just the very basic idea, I still have to think about some things
> and implementation issues.[/color]

This might be of value:

http://groups.google.com/groups?th=36072a520721e723

Slainte!

--
Jock
Michael Fesser
Guest
 
Posts: n/a
#22: Jul 17 '05

re: Noob - php works as .php but not when part of html


.oO(John Dunlop)
[color=blue]
>This might be of value:
>
>http://groups.google.com/groups?th=36072a520721e723[/color]

Yep. Thanks.

Micha
Closed Thread