Results 1 to 3 of 3

Thread: How to include files in PHP

  1. #1
    Join Date
    Oct 2008
    Posts
    54

    How to include files in PHP

    The code itself is very simple. It uses the structure of language "include". It may not take parentheses. This structure will allow you to include a PHP file to interpret. Here is an example to include the "haut.php" in the file:

    PHP Code:
    <?php 
    include 'haut.php' 
    ?>
    The principle is simple: you can go through the code execute PHP code included in "haut.php. This means that if for example the included file contains HTML code or instructions "echo" displaying HTML, you can display a well-defined HTML code on all pages of your sites through a single file that you include when and wherever you want.

    Now a practical example that will include a PHP page, the top of the site. All of our PHP pages can be reused so that the top site is the same on all pages. For this example we'll use two files, one will be HTML (the "haut.php") and will be the same on all pages for which we will call him, and one in PHP will include this file.

    File Contents "haut.php":

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" > 
    <html xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" lang= "en" > 
    <head>
    <title> Welcome to my website! </ title>
    </ head>
    <body>
    <h1> Welcome to my site! </ h1>

    File Contents "bas.php" which will be the footer of the site: 
    </body> 
    </html> 

    Contents of the file "index.php" to be the home: 
    <? php
    include 'haut.php';

    echo 
    'Here the content of the site';

    include 
    'bas.php';
    ?> 

    Giving in HTML when you call the page "index.php" 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" > 
    <html xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" lang= "en" > 
    <head> 
    <title> Welcome to my website! </ title>
    </ head>
    <body>
    <h1> Welcome to my site! </ h1>
    Here the content of the site
    </body> 
    </html>
    Attention to the use abundance files included. Include the more you use, the more time to generate the page will be slow (because it is necessary to make a disk access to read the files). Prefer to include one of a file a little larger than 4 files very small.

  2. #2
    Join Date
    Oct 2008
    Posts
    54

    Re: How to include files in PHP

    Require inclusion with

    the structure of language require works the same way include. However, there is a difference between these two methods. When you try to include a file if the file is not that happening? And by default, PHP will display an error but will continue running the script. Now imagine that you have a member who, in an included file, verify that the user is properly identified as a member to view the rest. Here is a summary example (the code is not complete):


    PHP Code:
    <? php
    include 'verification.php';

    / *
    Code to be executed if the member is identified
    * /

    echo 
    'you are a member! " ;
    >
    And now, here are the contents of the file "verification.php" which is responsible for verifying if the user is identified as a member:

    PHP Code:
        
    <? php
    if (! est_member ())
    (
    exit ( 
    'You must be a member to access this page');
    )
    >

  3. #3
    Join Date
    Oct 2008
    Posts
    54

    Re: How to include files in PHP

    The code is of course very simplified, we consider that the est_member () is higher in the code) The only goal is to understand what can cause the lack of inclusion of sensitive files. Here, we use the function est_member () which returns TRUE if the user is identified as a member, and FALSE otherwise. The exclamation point can make the opposite of the condition, here is translated by "if the user is not a member" and that without the exclamation point on would have said "if the user is a member. The code stops running the PHP script if the visitor is not a member by using the structure of language exit. All this is fine, but if the file "verification.php" does not exist in this case, PHP will display "you are a member!" what is wrong at all with what you want done. We will use the structure of language "require". It allows you to include a file and cut the script if the file has been included.

    PHP Code:
    <? php
    require 'verification.php' / / Cut the code if the file has been included by PHP

    / *
    Code to be executed if the member is identified
    * /

    echo 
    'you are a member! " ;
    ?> Has <? Php
    require '
    verification.php' / / Cut the code if the file has been included by PHP

    / *
    * Code to be executed if the member is identified
    * /

    echo '
    you are a member" ;
    >

Similar Threads

  1. How to include .SWF files to the html webpage
    By LynDa55 in forum Technology & Internet
    Replies: 3
    Last Post: 15-01-2011, 07:02 PM
  2. How to use include() in PHP?
    By Emily123 in forum Software Development
    Replies: 6
    Last Post: 06-03-2010, 05:49 AM
  3. Not able to Include files dynamically in ASP
    By DANIEL 602 in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 01:32 AM
  4. How to Use the #include Directive in ASP?
    By NIcaBoy in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 06:24 AM
  5. include() v/s require()
    By Anikait in forum Software Development
    Replies: 4
    Last Post: 25-03-2009, 10:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,961,796.69136 seconds with 17 queries