f



Parsing a php include (which also contains php code) - or "Reparsing" the php file

Hello,

I am using the <?php include() ?> statement on my website for
organizational purposes.  However, one of my includes contains some
PHP code.  Is there any way for the server to actually parse the
include?  I've tried this before, and it did not parse the include.
Rather, it included the file as just plain ASCII.
=======================
/*EXAMPLE 1*/
/*index.php*/
....
<?php include('global/includes/footer.inc') ?>
....

/*footer.inc*/
....
<p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
reserved.</p>
....

/*EXAMPLE 2*/
/*index.php*/
....
<?php include('global/includes/lastmod.php') ?>
....

/*lastmod.php*/
....
<?php
   echo "This file was last modified: ";
   echo strftime("%A %B %d %Y");
   include('whateverfilename.inc');
?>

=============================

I would like to be able to parse the include if it has php code, and
in some cases, create nesting includes (an include within an
include).  Is this even possible?  Any ideas?

Any help is certainly appreciated.  Thanks!

Steven Borrelli
Web Developer
Kingswood School

0
sborrelli8 (28)
6/20/2007 3:16:19 AM
comp.lang.php 32646 articles. 0 followers. Post Follow

18 Replies
1388 Views

Similar Articles

[PageSpeed] 2

Steven Borrelli wrote:
> Hello,
> 
> I am using the <?php include() ?> statement on my website for
> organizational purposes.  However, one of my includes contains some
> PHP code.  Is there any way for the server to actually parse the
> include?  I've tried this before, and it did not parse the include.
> Rather, it included the file as just plain ASCII.

If the main page is a php file, include will also make the included page to be
parsed by php.


I do suggest you use semicolon after each command, no matter if it's alone
between the php-tags or not.


It's always better to use .php as the file extension on the included files
too, this as .inc normally don't be set in the server to be parsed, which
leads to that if a person gives and url like

http://example.net/footer.inc

they will see the full source, and if you happen to have your database
login/password stored in the file, then they will know how to access it.


If you don't already, it's quite good to run apache web server and avoid iss.

-- 

  //Aho
0
user7 (4036)
6/20/2007 4:40:27 AM
In article <1182309379.784775.138420@q75g2000hsh.googlegroups.com>,
 Steven Borrelli <sborrelli8@gmail.com> wrote:

> Hello,
> 
> I am using the <?php include() ?> statement on my website for
> organizational purposes.  However, one of my includes contains some
> PHP code.  Is there any way for the server to actually parse the
> include?  I've tried this before, and it did not parse the include.
> Rather, it included the file as just plain ASCII.
> =======================
> /*EXAMPLE 1*/
> /*index.php*/
> ...
> <?php include('global/includes/footer.inc') ?>
> ...
> 
> /*footer.inc*/
> ...
> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
> reserved.</p>
> ...
> 
> /*EXAMPLE 2*/
> /*index.php*/
> ...
> <?php include('global/includes/lastmod.php') ?>
> ...
> 
> /*lastmod.php*/
> ...
> <?php
>    echo "This file was last modified: ";
>    echo strftime("%A %B %d %Y");
>    include('whateverfilename.inc');
> ?>
> 
> =============================
> 
> I would like to be able to parse the include if it has php code, and
> in some cases, create nesting includes (an include within an
> include).  Is this even possible?  Any ideas?
> 
> Any help is certainly appreciated.  Thanks!
> 
> Steven Borrelli
> Web Developer
> Kingswood School

Should be parsed as long as the include file has the <? and ?> tags. 
Most of my includes are tagged .inc and it works fine.
0
tim.streater (535)
6/20/2007 8:59:59 AM
Tim Streater wrote:
> In article <1182309379.784775.138420@q75g2000hsh.googlegroups.com>,
>  Steven Borrelli <sborrelli8@gmail.com> wrote:
> 
>> Hello,
>>
>> I am using the <?php include() ?> statement on my website for
>> organizational purposes.  However, one of my includes contains some
>> PHP code.  Is there any way for the server to actually parse the
>> include?  I've tried this before, and it did not parse the include.
>> Rather, it included the file as just plain ASCII.
>> =======================
>> /*EXAMPLE 1*/
>> /*index.php*/
>> ...
>> <?php include('global/includes/footer.inc') ?>
>> ...
>>
>> /*footer.inc*/
>> ...
>> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
>> reserved.</p>
>> ...
>>
>> /*EXAMPLE 2*/
>> /*index.php*/
>> ...
>> <?php include('global/includes/lastmod.php') ?>
>> ...
>>
>> /*lastmod.php*/
>> ...
>> <?php
>>    echo "This file was last modified: ";
>>    echo strftime("%A %B %d %Y");
>>    include('whateverfilename.inc');
>> ?>
>>
>> =============================
>>
>> I would like to be able to parse the include if it has php code, and
>> in some cases, create nesting includes (an include within an
>> include).  Is this even possible?  Any ideas?
>>
>> Any help is certainly appreciated.  Thanks!
>>
>> Steven Borrelli
>> Web Developer
>> Kingswood School
> 
> Should be parsed as long as the include file has the <? and ?> tags. 
> Most of my includes are tagged .inc and it works fine.

It depends completely on how your webserver is set up.

But .php files should be parsed by every webserver which has php installed.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
6/20/2007 11:40:51 AM
On 20.06.2007 13:40 Jerry Stuckle wrote:
> Tim Streater wrote:
>> In article <1182309379.784775.138420@q75g2000hsh.googlegroups.com>,
>>  Steven Borrelli <sborrelli8@gmail.com> wrote:
>>
>>> Hello,
>>>
>>> I am using the <?php include() ?> statement on my website for
>>> organizational purposes.  However, one of my includes contains some
>>> PHP code.  Is there any way for the server to actually parse the
>>> include?  I've tried this before, and it did not parse the include.
>>> Rather, it included the file as just plain ASCII.
>>> =======================
>>> /*EXAMPLE 1*/
>>> /*index.php*/
>>> ...
>>> <?php include('global/includes/footer.inc') ?>
>>> ...
>>>
>>> /*footer.inc*/
>>> ...
>>> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
>>> reserved.</p>
>>> ...
>>>
>>> /*EXAMPLE 2*/
>>> /*index.php*/
>>> ...
>>> <?php include('global/includes/lastmod.php') ?>
>>> ...
>>>
>>> /*lastmod.php*/
>>> ...
>>> <?php
>>>    echo "This file was last modified: ";
>>>    echo strftime("%A %B %d %Y");
>>>    include('whateverfilename.inc');
>>> ?>
>>>
>>> =============================
>>>
>>> I would like to be able to parse the include if it has php code, and
>>> in some cases, create nesting includes (an include within an
>>> include).  Is this even possible?  Any ideas?
>>>
>>> Any help is certainly appreciated.  Thanks!
>>>
>>> Steven Borrelli
>>> Web Developer
>>> Kingswood School
>>
>> Should be parsed as long as the include file has the <? and ?> tags. 
>> Most of my includes are tagged .inc and it works fine.
> 
> It depends completely on how your webserver is set up.
> 
> But .php files should be parsed by every webserver which has php installed.
> 

You're confusing php include() and server-side includes or whatever. php 
include() doesn't involve the web server (unless you're including a http 
url).

-- 
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
0
stereofrog (317)
6/20/2007 2:08:05 PM
On 20.06.2007 05:16 Steven Borrelli wrote:
> Hello,
> 
> I am using the <?php include() ?> statement on my website for
> organizational purposes.  However, one of my includes contains some
> PHP code.  Is there any way for the server to actually parse the
> include?  I've tried this before, and it did not parse the include.
> Rather, it included the file as just plain ASCII.
> =======================
> /*EXAMPLE 1*/
> /*index.php*/
> ...
> <?php include('global/includes/footer.inc') ?>
> ...
> 
> /*footer.inc*/
> ...
> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
> reserved.</p>
> ...
> 
> /*EXAMPLE 2*/
> /*index.php*/
> ...
> <?php include('global/includes/lastmod.php') ?>
> ...
> 
> /*lastmod.php*/
> ...
> <?php
>    echo "This file was last modified: ";
>    echo strftime("%A %B %d %Y");
>    include('whateverfilename.inc');
> ?>
> 
> =============================
> 
> I would like to be able to parse the include if it has php code, and
> in some cases, create nesting includes (an include within an
> include).  Is this even possible?  Any ideas?
> 
> Any help is certainly appreciated.  Thanks!
> 
> Steven Borrelli
> Web Developer
> Kingswood School
> 

Actually, includes are always parsed and your code should work 
perfectly. Double-check your configuration.

-- 
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
0
stereofrog (317)
6/20/2007 2:12:24 PM
gosha bine wrote:
> On 20.06.2007 13:40 Jerry Stuckle wrote:
>> Tim Streater wrote:
>>> In article <1182309379.784775.138420@q75g2000hsh.googlegroups.com>,
>>>  Steven Borrelli <sborrelli8@gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> I am using the <?php include() ?> statement on my website for
>>>> organizational purposes.  However, one of my includes contains some
>>>> PHP code.  Is there any way for the server to actually parse the
>>>> include?  I've tried this before, and it did not parse the include.
>>>> Rather, it included the file as just plain ASCII.
>>>> =======================
>>>> /*EXAMPLE 1*/
>>>> /*index.php*/
>>>> ...
>>>> <?php include('global/includes/footer.inc') ?>
>>>> ...
>>>>
>>>> /*footer.inc*/
>>>> ...
>>>> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
>>>> reserved.</p>
>>>> ...
>>>>
>>>> /*EXAMPLE 2*/
>>>> /*index.php*/
>>>> ...
>>>> <?php include('global/includes/lastmod.php') ?>
>>>> ...
>>>>
>>>> /*lastmod.php*/
>>>> ...
>>>> <?php
>>>>    echo "This file was last modified: ";
>>>>    echo strftime("%A %B %d %Y");
>>>>    include('whateverfilename.inc');
>>>> ?>
>>>>
>>>> =============================
>>>>
>>>> I would like to be able to parse the include if it has php code, and
>>>> in some cases, create nesting includes (an include within an
>>>> include).  Is this even possible?  Any ideas?
>>>>
>>>> Any help is certainly appreciated.  Thanks!
>>>>
>>>> Steven Borrelli
>>>> Web Developer
>>>> Kingswood School
>>>
>>> Should be parsed as long as the include file has the <? and ?> tags. 
>>> Most of my includes are tagged .inc and it works fine.
>>
>> It depends completely on how your webserver is set up.
>>
>> But .php files should be parsed by every webserver which has php 
>> installed.
>>
> 
> You're confusing php include() and server-side includes or whatever. php 
> include() doesn't involve the web server (unless you're including a http 
> url).
> 

No, I'm not.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
6/20/2007 6:05:03 PM
Jerry Stuckle wrote:

> It depends completely on how your webserver is set up.
> 
> But .php files should be parsed by every webserver which has php installed.

All files included with include(), require(), include_once() and
require_once() are parsed as PHP files. This is not dependent on
the filename or web server.

-- 
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 10:37.]

                             dict, thes & ency
          http://tobyinkster.co.uk/blog/2007/06/18/dict-thes-ency/
0
6/21/2007 7:02:54 AM
Toby A Inkster wrote:
> Jerry Stuckle wrote:
> 
>> It depends completely on how your webserver is set up.
>>
>> But .php files should be parsed by every webserver which has php installed.
> 
> All files included with include(), require(), include_once() and
> require_once() are parsed as PHP files. This is not dependent on
> the filename or web server.
> 

Toby,

Interesting, because I've seen this before, and others have commented on 
it, also.  Maybe it was a bug in an older version of PHP - it's been a 
while ago.  But I had one local cohort who was using php includes (not 
SSI), and his files were not being parsed when they had .inc extensions.

Changing them to .php worked.  And no, I'm sure he didn't use SSI.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
6/21/2007 11:27:52 AM
..oO(Jerry Stuckle)

>Interesting, because I've seen this before, and others have commented on 
>it, also.  Maybe it was a bug in an older version of PHP - it's been a 
>while ago.  But I had one local cohort who was using php includes (not 
>SSI), and his files were not being parsed when they had .inc extensions.

Either a bug or something heavily wrong on the server. As mentioned in
another post -- PHP includes have _nothing_ to do with the web server,
they are entirely handled by the interpreter. Since you explicitly write
their name in the include statement, you can call them whatever you
like:

require_once 'foo.bar';
require_once '42';
require_once '.o0o.';

All valid.

Micha
0
netizen9200 (1826)
6/21/2007 1:31:25 PM
On 20 Jun, 12:40, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Tim Streater wrote:
> > In article <1182309379.784775.138...@q75g2000hsh.googlegroups.com>,
> >  Steven Borrelli <sborrel...@gmail.com> wrote:
>
> >> Hello,
>
> >> I am using the <?php include() ?> statement on my website for
> >> organizational purposes.  However, one of my includes contains some
> >> PHP code.  Is there any way for the server to actually parse the
> >> include?  I've tried this before, and it did not parse the include.
> >> Rather, it included the file as just plain ASCII.
> >> =======================
> >> /*EXAMPLE 1*/
> >> /*index.php*/
> >> ...
> >> <?php include('global/includes/footer.inc') ?>
> >> ...
>
> >> /*footer.inc*/
> >> ...
> >> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
> >> reserved.</p>
> >> ...
>
> >> /*EXAMPLE 2*/
> >> /*index.php*/
> >> ...
> >> <?php include('global/includes/lastmod.php') ?>
> >> ...
>
> >> /*lastmod.php*/
> >> ...
> >> <?php
> >>    echo "This file was last modified: ";
> >>    echo strftime("%A %B %d %Y");
> >>    include('whateverfilename.inc');
> >> ?>
>
> >> =============================
>
> >> I would like to be able to parse the include if it has php code, and
> >> in some cases, create nesting includes (an include within an
> >> include).  Is this even possible?  Any ideas?
>
> >> Any help is certainly appreciated.  Thanks!
>
> >> Steven Borrelli
> >> Web Developer
> >> Kingswood School
>
> > Should be parsed as long as the include file has the <? and ?> tags.
> > Most of my includes are tagged .inc and it works fine.
>
> It depends completely on how your webserver is set up.
>

Not for an includED file - the web server has no visibility of it.

C.


0
6/21/2007 4:18:58 PM
C. wrote:
> On 20 Jun, 12:40, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Tim Streater wrote:
>>> In article <1182309379.784775.138...@q75g2000hsh.googlegroups.com>,
>>>  Steven Borrelli <sborrel...@gmail.com> wrote:
>>>> Hello,
>>>> I am using the <?php include() ?> statement on my website for
>>>> organizational purposes.  However, one of my includes contains some
>>>> PHP code.  Is there any way for the server to actually parse the
>>>> include?  I've tried this before, and it did not parse the include.
>>>> Rather, it included the file as just plain ASCII.
>>>> =======================
>>>> /*EXAMPLE 1*/
>>>> /*index.php*/
>>>> ...
>>>> <?php include('global/includes/footer.inc') ?>
>>>> ...
>>>> /*footer.inc*/
>>>> ...
>>>> <p>&copy 1993-<?php echo date("Y") ?> Kingswood School.  All rights
>>>> reserved.</p>
>>>> ...
>>>> /*EXAMPLE 2*/
>>>> /*index.php*/
>>>> ...
>>>> <?php include('global/includes/lastmod.php') ?>
>>>> ...
>>>> /*lastmod.php*/
>>>> ...
>>>> <?php
>>>>    echo "This file was last modified: ";
>>>>    echo strftime("%A %B %d %Y");
>>>>    include('whateverfilename.inc');
>>>> ?>
>>>> =============================
>>>> I would like to be able to parse the include if it has php code, and
>>>> in some cases, create nesting includes (an include within an
>>>> include).  Is this even possible?  Any ideas?
>>>> Any help is certainly appreciated.  Thanks!
>>>> Steven Borrelli
>>>> Web Developer
>>>> Kingswood School
>>> Should be parsed as long as the include file has the <? and ?> tags.
>>> Most of my includes are tagged .inc and it works fine.
>> It depends completely on how your webserver is set up.
>>
> 
> Not for an includED file - the web server has no visibility of it.

It's true, the file extension don't matter when you include files in a php
file, but by default a *.inc file won't be parsed if it's directly accessed

example: http://www.example.net/myincludefile.inc

This can be a security issue if you store database login/passwords in a *.inc
file, which you should avoid to use any other extention than *.php, which will
be parsed on a php enabled server.

-- 

  //Aho
0
user7 (4036)
6/21/2007 8:11:45 PM
..oO(J.O. Aho)

>It's true, the file extension don't matter when you include files in a php
>file, but by default a *.inc file won't be parsed if it's directly accessed
>
>example: http://www.example.net/myincludefile.inc
>
>This can be a security issue if you store database login/passwords in a *.inc
>file, which you should avoid to use any other extention than *.php, which will
>be parsed on a php enabled server.

Of course these files should be stored outside the document root.

Even a .php extension is no guarantee that no visitors will ever be able
to view that file. A server update, a misconfiguration, whatever --
there are some situations where even a .php file could be delivered
unparsed.

Micha
0
netizen9200 (1826)
6/22/2007 1:05:45 AM
On Jun 21, 8:31 am, Michael Fesser <neti...@gmx.de> wrote:
> .oO(Jerry Stuckle)
>
> >Interesting, because I've seen this before, and others have commented on
> >it, also.  Maybe it was a bug in an older version of PHP - it's been a
> >while ago.  But I had one local cohort who was using php includes (not
> >SSI), and his files were not being parsed when they had .inc extensions.
>
> Either a bug or something heavily wrong on the server. As mentioned in
> another post -- PHP includes have _nothing_ to do with the web server,
> they are entirely handled by the interpreter. Since you explicitly write
> their name in the include statement, you can call them whatever you
> like:
>
> require_once 'foo.bar';
> require_once '42';
> require_once '.o0o.';
>
> All valid.
>
> Micha

When you say the PHP includes have nothing to do with the web server,
that is somewhat confusing.

If I have my index.php, and I have a call to a php include within
that, and also another php include within the first include, and all
the includes have the .php file extension, will everything be parsed
and appear like it's supposed to?  And it won't work if the file
extension is different?

In other words, is it possible to make nesting php includes?

Steven

0
sborrelli8 (28)
6/22/2007 2:46:45 PM
Steven Borrelli wrote:

> When you say the PHP includes have nothing to do with the web server,
> that is somewhat confusing.

The web server won't load the included files, it won't have a clue what has
been included, as this is handled by the PHP parser module.


> If I have my index.php, and I have a call to a php include within
> that, and also another php include within the first include, and all
> the includes have the .php file extension, will everything be parsed
> and appear like it's supposed to?

The extension has nothing to do with if the file will be parsed or not, but of
"security" reasons it's vise to set the php extension to the include files too.

When you include a php script, see to that the PHP code in that file has the
'<?PHP' and '?>' tags, or else the php code will be treated as plain text.

example:

--- file1.inc.php ---
<?PHP
	echo "Hello";
?>
--- eof ---


--- file2.inc.php ---
	echo "Hello";
--- eof ---


--- index.php ---
<html><head><title>php include test</title></head><body>
First include: <?PHP include 'file1.inc.php'; ?><br>
Second include: <?PHP include 'file2.inc.php'; ?><br>
</body></html>
--- eof ---


If you run this small example you will notice that the output will look like this:

First include: Hello
Second include: echo "Hello";



> In other words, is it possible to make nesting php includes?

Sure you can, there is no problems with that at all and it's justed quite a
lot on the most big projects you can find (see freshmeat.net).


-- 

  //Aho
0
user7 (4036)
6/22/2007 3:18:53 PM
you can even leave off the ?> in the include file if you like - as
long as you have the <? at the start

0
ptmcnally (269)
6/23/2007 1:46:00 AM
On Jun 22, 10:18 am, "J.O. Aho" <u...@example.net> wrote:
> Steven Borrelli wrote:
> > When you say the PHP includes have nothing to do with the web server,
> > that is somewhat confusing.
>
> The web server won't load the included files, it won't have a clue what has
> been included, as this is handled by the PHP parser module.
>
> > If I have my index.php, and I have a call to a php include within
> > that, and also another php include within the first include, and all
> > the includes have the .php file extension, will everything be parsed
> > and appear like it's supposed to?
>
> The extension has nothing to do with if the file will be parsed or not, but of
> "security" reasons it's vise to set the php extension to the include files too.
>
> When you include a php script, see to that the PHP code in that file has the
> '<?PHP' and '?>' tags, or else the php code will be treated as plain text.
>
> example:
>
> --- file1.inc.php ---
> <?PHP
>         echo "Hello";
> ?>
> --- eof ---
>
> --- file2.inc.php ---
>         echo "Hello";
> --- eof ---
>
> --- index.php ---
> <html><head><title>php include test</title></head><body>
> First include: <?PHP include 'file1.inc.php'; ?><br>
> Second include: <?PHP include 'file2.inc.php'; ?><br>
> </body></html>
> --- eof ---
>
> If you run this small example you will notice that the output will look like this:
>
> First include: Hello
> Second include: echo "Hello";
>
> > In other words, is it possible to make nesting php includes?
>
> Sure you can, there is no problems with that at all and it's justed quite a
> lot on the most big projects you can find (see freshmeat.net).
>
> --
>
>   //Aho

That makes sense now.  Thanks so much Aho, ... and everyone else too!

Steven

0
sborrelli8 (28)
6/23/2007 4:32:20 AM
On Jun 23, 4:46 am, macca <ptmcna...@googlemail.com> wrote:
> you can even leave off the ?> in the include file if you like - as
> long as you have the <? at the start

You can leave it out in any php-file, whether it is included or not.
As long as there isn't any output after that. However, I would not
recommend this, as it can be considered as sloppy coding practise.

--
Jussi

Guy calls a government office, and proclaims "I want to be instated as
the president!" He gets the reply "Are you an idiot, or just mentally
ill?" The guy goes "Yes, yes! And I'm old, too!"

disczero.com
naamio.net
hoffburger.com


0
jussist (23)
6/23/2007 9:18:41 AM
..oO(J.O. Aho)

>Steven Borrelli wrote:
>
>> When you say the PHP includes have nothing to do with the web server,
>> that is somewhat confusing.
>
>The web server won't load the included files, it won't have a clue what has
>been included, as this is handled by the PHP parser module.

Yep. You can even use includes in command line scripts (PHP-CLI), where
absolutely no webserver is involved. It's always a direct file access,
done by the PHP interpreter itself.

>> If I have my index.php, and I have a call to a php include within
>> that, and also another php include within the first include, and all
>> the includes have the .php file extension, will everything be parsed
>> and appear like it's supposed to?
>
>The extension has nothing to do with if the file will be parsed or not, but of
>"security" reasons it's vise to set the php extension to the include files too.

Depends on where the files are stored. It's much more secure to put
include files _outside_ of the webserver's document root, so they can't
be accessed by a URL at all.

Micha
0
netizen9200 (1826)
6/24/2007 4:57:56 PM
Reply:

Web resources about - Parsing a php include (which also contains php code) - or "Reparsing" the php file - comp.lang.php

Resources last updated: 3/23/2016 8:04:05 PM