Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PHPGoto page 1, 2, 3  Next

php include design question

 
Jump to:  
 
Mark
PostPosted: Wed Aug 13, 2008 11:41 pm    Post subject: php include design question
       
so, i'm making a website. let's say i have header.php, footer.php and
content.php.

now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?

i suppose i can include a variable in header.php that basically says
"i've been included", and then in content.php if this variable isn't
set, i could include them... is this the best/most elegant approach?
 

 
J2Be.com
PostPosted: Wed Aug 13, 2008 11:41 pm    Post subject: Re: php include design question
       
"Mark" <mnbayazit@gmail.com> ha scritto nel messaggio
news:3a5a9663-281a-4717-a362-eeb7570c6462@p31g2000prf.googlegroups.com...
Quote:
so, i'm making a website. let's say i have header.php, footer.php and
content.php.

now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?

i suppose i can include a variable in header.php that basically says
"i've been included", and then in content.php if this variable isn't
set, i could include them... is this the best/most elegant approach?



example
--index.php--
<?php
define('is_inc',true);
readfile('header.php');
include('content.php');
readfile('footer.php');
?>

--content.php--
<?php
if(!defined('is_inc')){exit();}
echo 'mycontent';
?>
---------------------

Yes the variable or define methods are the most used.

Regards

L. A. Iarrusso
 

 
Floortje
PostPosted: Thu Aug 14, 2008 4:58 am    Post subject: Re: php include design question
       
Mark wrote:
Quote:
--content.php--
if(!isset('dont_load')) include 'header.php';
// content
if(!isset('dont_load')) include 'footer.php';

And then when I sub a page in w/ ajax I just call it with content.php?
dont_load=1

That should work right? Sorry I wasn't more clear the first time.

I think you are patching one bad choice with another but I dont really
have the time to explain.
Why dont you have a look at the larger framworks and see how they have
done it.

Floortje
 

 
Mark
PostPosted: Thu Aug 14, 2008 5:36 am    Post subject: Re: php include design question
       
On Aug 13, 6:31 pm, "J2Be.com" <mym...@virgilio.it> wrote:
Quote:
"Mark" <mnbaya...@gmail.com> ha scritto nel messaggionews:3a5a9663-281a-4717-a362-eeb7570c6462@p31g2000prf.googlegroups.com...

so, i'm making a website. let's say i have header.php, footer.php and
content.php.

now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?

i suppose i can include a variable in header.php that basically says
"i've been included", and then in content.php if this variable isn't
set, i could include them... is this the best/most elegant approach?

example
--index.php--
?php
define('is_inc',true);
readfile('header.php');
include('content.php');
readfile('footer.php');
?

--content.php--
?php
if(!defined('is_inc')){exit();}
echo 'mycontent';
?
---------------------

Yes the variable or define methods are the most used.

Regards

L. A. Iarrusso

In your scenario accessing content.php directly outputs nothing?

I had more of this in mind:

--header.php--
define('header_inc',true);
// header stuffs

--content.php--
if(!defined('header_inc')) include 'header.php';
echo 'mycontent';
if(!defined('footer_inc')) include 'footer.php';

--index.php--
include 'header.php';
include 'content.php';
include 'footer.php';


Of course, this pretty much makes index.php and content.php identical,
but the idea was that after the header and footer are loaded once, I
could use ajax to sub in different content pages instead of reloading
the whole page...

But thinking about this more...that wouldn't work. If I used ajax to
include the new content, header_inc wouldn't be defined and I'd wind
up with 2 headers and footers.

I guess what I'd have to do is include a variable in the URL in my
ajax call?

Basically...

--content.php--
if(!isset('dont_load')) include 'header.php';
// content
if(!isset('dont_load')) include 'footer.php';

And then when I sub a page in w/ ajax I just call it with content.php?
dont_load=1

That should work right? Sorry I wasn't more clear the first time.
 

 
NC
PostPosted: Thu Aug 14, 2008 6:12 am    Post subject: Re: php include design question
       
On Aug 13, 4:41 pm, Mark <mnbaya...@gmail.com> wrote:
Quote:

so, i'm making a website. let's say i have header.php,
footer.php and content.php.

i suppose i can include a variable in header.php that
basically says "i've been included", and then in content.php
if this variable isn't set, i could include them... is this
the best/most elegant approach?

Elegance is in the eye of the beholder, so I can't really say whether
or not this is the most elegant approach. There is, however, an
alternative. You can put header.php, footer.php and content.php into
a subdirectory and disable HTTP access to it with .htaccess.

This approach allows for easy theming. Say, right now you have:

$themeName = 'default';
$themeDir = 'themes/' . $themeDir;
include "$themeDir/header.php";
include "$themeDir/content.php";
include "$themeDir/footer.php";

Then you can work out a new set of content display files, put them
into a different directory, say, themes/new, and switch the new theme
on by changing a single line (the value of $themeName) in index.php...

Cheers,
NC
 

 
Michael Fesser
PostPosted: Thu Aug 14, 2008 3:30 pm    Post subject: Re: php include design question
       
..oO(Mark)

Quote:
so, i'm making a website. let's say i have header.php, footer.php and
content.php.

now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?

Why do you want the content.php to be callable? Includes should be
stored outside the document root, so that they can't be reached by a
URL. Then you won't have this problem at all.

Micha
 

 
Michael Fesser
PostPosted: Thu Aug 14, 2008 5:10 pm    Post subject: Re: php include design question
       
..oO(Mark)

Quote:
On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de> wrote:
.oO(Mark)

so, i'm making a website. let's say i have header.php, footer.php and
content.php.

now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?

Why do you want the content.php to be callable? Includes should be
stored outside the document root, so that they can't be reached by a
URL. Then you won't have this problem at all.

Micha

Well, how else are people going to bookmark a particular page, or link
it to their friends? If I don't make it accessible somehow, the only
way people will be able to get to it is by going through the index.

It's still not exactly clear what you want. If the content.php just
contains the content of a single page, then it should not be directly
callable, as said. But if it's an entire page instead, then there's no
point in including it in the index.php ... what am I missing?

Usually it's like this: You have a bunch of pages, and each page simply
includes the header.php and footer.php (I would rather name them *.inc,
but that's just personal preference). Of course these page scripts have
to be publically available, but not the included header and footer files
because they are not meant to be called directly.

Micha
 

 
Michael Fesser
PostPosted: Thu Aug 14, 2008 5:57 pm    Post subject: Re: php include design question
       
..oO(Captain Paralytic)

Quote:
Of course the problem with calling them *.inc is that anyone calling
it from their browser will see any php code within them.

In the worst case the same can happen with *.php and as I've already
said twice in this thread - such files don't belong to the document
root. Put them one level higher: no URL, no problem.

Micha
 

 
Jerry Stuckle
PostPosted: Thu Aug 14, 2008 6:23 pm    Post subject: Re: php include design question
       
Mark wrote:
Quote:
On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de> wrote:
.oO(Mark)

so, i'm making a website. let's say i have header.php, footer.php and
content.php.
now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?
Why do you want the content.php to be callable? Includes should be
stored outside the document root, so that they can't be reached by a
URL. Then you won't have this problem at all.

Micha

Well, how else are people going to bookmark a particular page, or link
it to their friends? If I don't make it accessible somehow, the only
way people will be able to get to it is by going through the index.


Don't confuse the file with the page. You can store a file outside of
the document root and still include it in a page.

But if index.php includes is the only page ever called, how are you
going to determine which file to include for your content?



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 

 
Mark
PostPosted: Thu Aug 14, 2008 6:29 pm    Post subject: Re: php include design question
       
On Aug 14, 10:30 am, Michael Fesser <neti...@gmx.de> wrote:
Quote:
.oO(Mark)

so, i'm making a website. let's say i have header.php, footer.php and
content.php.

now in index.php I simply want to include the 3 pages. easy enough to
do.
but let's say the user navigates to mysite.com/content.php. now the
header and footer will appear to be missing.
so now the question is, how can i include the header and footer in
content.php only if the page isn't already nested?

Why do you want the content.php to be callable? Includes should be
stored outside the document root, so that they can't be reached by a
URL. Then you won't have this problem at all.

Micha

Well, how else are people going to bookmark a particular page, or link
it to their friends? If I don't make it accessible somehow, the only
way people will be able to get to it is by going through the index.
 

Page 1 of 3 .:. Goto page 1, 2, 3  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Kolonie dla dzieci tonery najlepsze filmy karty plastikowe buty narciarskie