john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

php include require

php-include-require

include will have the server insert the contents of a file in your current file
before parsing it...

If an error occurs, the include() function generates a warning, but the script will continue. "require()" does the same thing but will halt on errors.

EXAMPLES:

<?php      include("head.txt");     ?>
<?php      include("footer.php");     ?>

so

main.php
<?php
    include some-function.txt
    some_function();
?>

some-function.txt
<?php


?>

To ensure that a piece of code is only added once...
 include_once or the require_once


 <?php
    $servername = $_SERVER['SERVER_NAME'];
    $filename = "http://" . $servername . "/head.txt";
    include($filename);
?>


<?php
    $servername = $_SERVER['SERVER_NAME'];
    $filename = "http://" . $servername . "/foot.txt";
    include($filename);
?>


-------------------------------------

PHP 5 improves default security settings so that "include" does not allow random url's

A better way then is:

$path_to_be = $_SERVER["DOCUMENT_ROOT"];
echo $path_to_be . "\n<br />";

include( $_SERVER["DOCUMENT_ROOT"] . "/includes/footer.htm");

  • « win button time
  • index file listing 2010 07 »

Published

Feb 9, 2010

Category

php

~122 words

Tags

  • include 6
  • php 82
  • require 3