php包括设计问题 [英] php include design question
问题描述
所以,我正在建立一个网站。让我们说我有header.php,footer.php和
content.php。
现在在index.php我只是想包括3页。很容易
。
但是让我们说用户导航到mysite.com/content.php。现在
的页眉和页脚似乎都会丢失。
现在的问题是,如何在页面和页脚中包含页眉和页脚
content.php只有在页面还没有嵌套的时候?
i假设我可以在header.php中包含一个变量,基本上就是说
i 已被包含在内,然后在content.php中如果这个变量没有设置,我可以包含它们......这是最好/最优雅的方法吗?
" Mark" < mn ******* @ gmail.comha scritto nel messaggio
news:3a ********************** ************ @ p31g2000 prf.googlegroups.com ...
所以,我正在建立一个网站。让我们说我有header.php,footer.php和
content.php。
现在在index.php我只是想包括3页。很容易
。
但是让我们说用户导航到mysite.com/content.php。现在
的页眉和页脚似乎都会丢失。
现在的问题是,如何在页面和页脚中包含页眉和页脚
content.php只有在页面还没有嵌套的时候?
i假设我可以在header.php中包含一个变量,基本上就是说
i 已被包含在内,然后在content.php中,如果此变量不是
设置,我可以包含它们......这是最好/最优雅的方法吗?
例子
--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'';
?>
---------------------
是的变量或定义方法是最常用的。
问候
LA Iarrusso >
8月13日,6:31 * pm,J2Be.com < mym ... @ virgilio.itwrote:
" Mark" < mnbaya ... @ gmail.comha scritto nel messaggionews:3a ********************************** @ p31g2000prf.googlegroups.com ...
所以,我正在建立一个网站。让我们说我有header.php,footer.php和
content.php。
现在在index.php中我只想包含3页。很容易
。
但是让我们说用户导航到mysite.com/content.php。现在
的页眉和页脚似乎都会丢失。
现在的问题是,如何在页面和页脚中包含页眉和页脚
content.php只有在页面还没有嵌套的情况下?
i假设我可以在header.php中包含一个变量,它基本上是说
我已被包括在内 ,然后在content.php中,如果这个变量没有设置,我可以包括它们......这是最好/最优雅的方法吗?
例子
--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 '';
?>
---------------------
是变量或定义方法是最常用的。
问候
LA Iarrusso
在你的场景中访问content.php直接输出什么?
我有更多这方面的想法:
--header.php--
define(''header_inc'',true);
//标题内容
--content.php--
if(!defined(''header_inc''))包含''header.php'';
echo''mycontent ';
if(!defined(''footer_inc''))包含''footer.php'';
--index.php--
包含''header.php'';
包含''content.php'';
包括''footer.php'' ;
当然,这几乎使index.php和content.php完全相同,
但是这个想法是在页眉和页脚加载一次之后,我是$>
可以在不同的内容页面中使用ajax来代替重新加载
整个页面......
但是考虑更多。那不行。如果我使用ajax
包含新内容,header_inc将无法定义,并且我将使用2个页眉和页脚来填充
。
我想我要做的是在我的
ajax电话中的URL中包含一个变量?
基本上......
--content.php--
if(!isset(''dont_load''))包含''header.php' ';
//内容
if(!isset(''dont_load''))包括''footer.php'';
那么当我用w / ajax分页时我只用content.php调用它?
dont_load = 1
这应该有效对?对不起,我第一次不清楚。
8月13日下午4:41,Mark< mnbaya ... @ gmail.comwrote:
>
所以,我正在建立一个网站。让我们说我有header.php,
footer.php和content.php。
i假设我可以在header.php中包含一个变量
基本上说我已被包括,然后在content.php
如果这个变量没有设置,我可以包括它们......这是
最好/最优雅的方法吗?
优雅是旁观者的眼睛,所以我不能说是否
或不是这是最优雅的方法。但是,有一个
替代方案。您可以将header.php,footer.php和content.php放入
a子目录中,并使用.htaccess禁用对它的HTTP访问。
此方法允许容易主题。说,现在你有:
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?
"Mark" <mn*******@gmail.comha scritto nel messaggio
news:3a**********************************@p31g2000 prf.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
On Aug 13, 6:31*pm, "J2Be.com" <mym...@virgilio.itwrote:"Mark" <mnbaya...@gmail.comha scritto nel messaggionews:3a********************************** @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. IarrussoIn 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.
On Aug 13, 4:41 pm, Mark <mnbaya...@gmail.comwrote:>
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:
这篇关于php包括设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!