Using PHP Include to Separate Site Content

Using PHP include to separate site content

Nope, your approach is wrong.

Here are main faults in your design:

  1. You're assuming that header.php would be called on the every page call. That's wrong.
  2. You're assuming that header.php will always be static. That's wrong.
  3. You forgot to create a template for the page itself.

The main rule everyone have to learn by heart:

Not a single character has to be sent into browser, until all data gets ready.

Why?

  • it's 2011 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
  • there is a thing called HTTP header. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.
  • it's for just 4-page site. Okay. Imagine you've got lucky and got a request for another 4-page site. You will have to change only templates and don't touch engine files. That's really great benefit.
  • Imagine you're going to make a custom <title> tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates.

So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.

An example layout is going to be like this:

.1. page itself.

it outputs nothing but only gather required data and calls a template:

<?php
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.tpl.php";
include "template.php";
?>

.2. template.php which is your main site template,

consists of your header and footer:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<?php include $tpl ?>
</div>
</body>
</html>

.3. and finally links.tpl.php is the actual page template:

<h2><?=$pagetitle?></h2>
<ul>
<?php foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<?php endforeach ?>
<ul>

easy, clean and maintainable.

How to include another website in another using PHP

$file = file_get_contents('http://www.address.com/something'); // Can be locally too, for example: something.php
echo $file;

Be carefull, this method is not 100% safe, until You are not 100% sure that the url you are echoing is safe.
Hope it helps.

How do you include multiple page content in one php include file?

It sounds like you are trying to avoid templates. The basic idea is to define a separate file for each of head, footer, nav and include those from your content template.

head.php

<!doctype html> <html> ... 

nav.php

<body> <a href=..> page1 </a> <a href=..> page2 </a> <a href=..> page3 </a>

foot.php

</body> </html>

And to create a complete page, you would do:

<?php
include 'head.php';
include 'nav.php'

An article about prune juice.

include 'foot.php';
?>

Now, if I understand correctly, you want to include only one file and be able to generate the various elements (head, nav, whathaveyou). You could do this by wrapping the include statements in functions:

ubertemplate

<?php
function head() { include 'head.php'; }
function nav() { include 'nav.php'; }

function randomElement() { ?>
An article about prune juice. <?php
}

function totalymisguideddynamiccontents() {
echo "<div> foobar <span> moo </span></div>"
}

function() { include 'foot.php'; }
?>

..then in your final page, call these functions:

uberpage

<?php

include 'ubertemplate.php';
?>

<?php head() ?>
<script src=superlib.js></src>
<?php nav() ?>

Yomama so big when she wears a yellow coat people yell TAXI!.

<?php foot() ?>

Finally, if I understand correctly and you think the uberpage approach seems like a good idea, ...then you should probably try it out. That may well be the only way to realize that it is flawed. I don't mean to mock your idea, but there are better ways to approach templates and the first way, the one you are trying to avoid, is cleaner (but that's only my opinion).

For a better way, look at twig templates or do some more research and find a framework that suits your needs.

How to I inject only certain parts of PHP page using PHP

The easiest (and usual) way is to simply make separate header.php and footer.php files, and access them where you need them. There's no direct support for only loading parts of a file.

Edit (to respond to your comment on the other answer about using separate functions): Let's say your file.php looks like this:

<?php

function header() { ?>
header content goes here
<?php }

function footer() { ?>
footer content goes here
<?php }

?>

Then in the page you're calling it into, you can use <?php header() ?> and <?php footer() ?> to produce the content where you want it.

include a website in php file

This code requires allow_url_include=On in your php.ini, which is disabled by default because it's REMOTE CODE EXECUTION, one of the worst you can have in PHP. This is called the Remote File Include (RFI) vulnerability. If there is PHP code on this site it will be executed on your server.

Extremely insecure:

<?php include("http://www.othersite.com/filename.html"); ?>

What you probably want is:

<?php print file_get_contents("http://www.othersite.com/filename.html"); ?>

However, this is technically an XSS vulnerability. So, if you trust the website there isn't a problem. But you probably want to run Html Purifer before printing it out.

Include pages to index.php or create top and bottom part of page and include to all content pages?

I recommend just using php includes for the header, nav, and footer elements and then placing a class (home, about me, contact, etc.) on the body tag (for highlighting nav elements and such). This way the content is on separate pages and gives you more freedom, but saves you from having to retype all of the navigation and stuff each time.

Example:

    <!DOCTYPE html>

<head>
<title>
Hello World
</title>
<meta name="description" content="Use this area to provide a description of this page.">
<?php include '_php/_includes/head.php'; ?>
</head>

<body class="home">

<?php include '_php/_includes/header.php'; ?>

<!--

Content Goes Here

Remember: 'div' elements should only be used for non-semantic styling purposes. Content should
be placed in either a 'section' or an 'article' tag.

-->

<?php include '_php/_includes/footer.php'; ?>

</body>

</html>

php include file from another directory

In your site's <head> section, insert a <base> element with the href attribute. Set the href attribute to the base URL of your website, and all relative requests will be sent through that base URL.

<base href="http://my-website-url.com/" />
<link rel="stylesheet" type="text/css" href="css/style.css" />

With a correctly-set base tag, the user's browser will attempt to load your stylesheet from the URL http://my-website-url.com/css/style.css.

Note: this not only affects stylesheets, but all relative links in the document.

Include one PHP file into another

Try including the file with an absolute path: something like this:

<?php include ($_SERVER['DOCUMENT_ROOT']."/folder1/global-functions.php");?>


Related Topics



Leave a reply



Submit