/    Sign up×
Community /Pin to ProfileBookmark

php include question – bad practice?

Hi everyone ? first post here. I’m hoping you guys could help me out:

I’m building a website and this is how I’ve done it:

Each page is located in a folder i.e /page1/ and in that folder is index.php (i’ve done this to hide the page file name which most websites seem to do these days)

Index.php assembles the webpage using a ton of includes:

include ‘../meta.php’;
include ‘../header.php’;
include ‘./content.php’;
include ‘../footer.php’;

meta is the title and metatags for the page
header is the menu and graphical flash interface
content is the content layout
footer is the footer

Is this bad practice? and will it affect the search engine optomization?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@malticFeb 07.2011 — "include './content.php';"

Do you mean: include '../content.php';?
Copy linkTweet thisAlerts:
@msolusauthorFeb 07.2011 — example: the folder "examplesite.com/page1" contains content.php, meta.php and index.php

the header and footer files are in the root directory of examplesite.com

index.php assembles using header and footer from the root directory and content and metatags from the directory "page1"

i'm just wondering if there is a better way to do this? is it bad practice? i have no techical training in web-design
Copy linkTweet thisAlerts:
@NogDogFeb 07.2011 — Search engines will be totally ignorant of how you assemble the output in your script. They see the same thing your browser sees: the resulting HTML received via a HTTP request.
Copy linkTweet thisAlerts:
@malticFeb 07.2011 — I'd like too add that I wouldn't call it bad practice. Although you might want too look at using require() for some things.
Copy linkTweet thisAlerts:
@tracknutFeb 07.2011 — I'm not following you on the purpose for the 'content' include, and frankly the meta include either. If those are unique files for each /page1/index.php file, what good is it doing to use an include to incorporate them in the file as opposed to just putting their contents directly in to the index.php file directly?

Dave
Copy linkTweet thisAlerts:
@NogDogFeb 07.2011 — I'm not following you on the purpose for the 'content' include, and frankly the meta include either. If those are unique files for each /page1/index.php file, what good is it doing to use an include to incorporate them in the file as opposed to just putting their contents directly in to the index.php file directly?

Dave[/QUOTE]


To expand a bit on that, what I often do is create one include file that has separate function definitions for each such part of the page. Those functions can then take arguments for things that vary from page to page, such as the title, meta description, and meta keywords. Then I require() that single file and call each function as needed.

include.php:
[code=php]
function page_head($title, $description='', $keywords='')
{
$html = "<html>
<head>
<title>%s</title>
<meta type='description' content='%s' />
<meta type='keywords' content='%s' />
</head>
<body>
";
return sprintf(
$html,
htmlentities($title, ENT_QUOTES),
htmlentities($description, ENT_QUOTES),
htmlentities($keywords, ENT_QUOTES)
);
}

function page_foot()
{
return "<p>Thus ends this page.</p>
</body>
</html>";
}
[/code]

sample page:
[code=php]
<?php
include "../include.php";
echo page_head('This Title', 'This is a test.', 'This,Test,Keywords');
?>
<!-- stuff unique to this page -->
<?php echo page_foot(); ?>
[/code]
Copy linkTweet thisAlerts:
@tracknutFeb 07.2011 — To expand a bit on that, what I often do is create one include file that has separate function definitions for each such part of the page. [/QUOTE]

Yep, I do that as well, and it's very handy. But unless I misread the OP's statement, he has separate include files for the content and meta includes for each web page. So my question was: why make an include file if you're only going to include it in one place?

Dave
Copy linkTweet thisAlerts:
@msolusauthorFeb 08.2011 — Yep, I do that as well, and it's very handy. But unless I misread the OP's statement, he has separate include files for the content and meta includes for each web page. So my question was: why make an include file if you're only going to include it in one place?

Dave[/QUOTE]


Haha you are completely right. I'm self teaching myself, and I guess it was my desire to compartmentalize everything. I've merged meta and index and content together with includes to the header and footer. Cheers


Thanks maltic, NogDog and tracknut for your help
×

Success!

Help @msolus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...