Announcement

Collapse
No announcement yet.

php includes

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • php includes

    To use PHP Includes, the first thing you will have to do is rename all your .html files to .php.

    After you've done that, the concept is pretty simple. Lets say you wanted to include navigation to all your pages. You would code your pages just like you would normally. When you reach the part in your page where your navigation would go, you use the include.

    PHP Code:
    <?php include("yournavfile.html"); ?>
    so lets say you had a page like this:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
    <title></title>
    </head>
    <body>
    
    <div id="wrap">
    	
    	<div id="top"></div>
    	
    	<div id="main">
    		
    		<ul id="nav">
    			<li><a href="page1.php">Link1</a></li>
    			<li><a href="page2.php">Link2</a></li>
    			<li><a href="page3.php">Link3</a></li>
    			<li><a href="page4.php">Link4</a></li>
    			<li><a href="page5.php">Link5</a></li>
    
    		</ul>
    		
    	<div id="footer"></div>
    
    </div>
    </body>
    </html>
    Instead of inserting your navigation into every one of your pages, you can just use the include from above.

    So now your page should look like this:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
    <title></title>
    </head>
    <body>
    
    <div id="wrap">
    	
    	<div id="top"></div>
    	
    	<div id="main">
    		
    		<ul id="nav">
    			<?php include("navigation.html"); ?>
    		</ul>
    		
    	<div id="footer"></div>
    
    </div>
    </body>
    </html
    The file that you are including (navigation.html) will contain the code that you want included:
    HTML Code:
    <li><a href="page1.php">Link1</a></li>
    <li><a href="page2.php">Link2</a></li>
    <li><a href="page3.php">Link3</a></li>
    <li><a href="page4.php">Link4</a></li>
    <li><a href="page5.php">Link5</a></li>

  • #2
    Okay, I'm a bit thick on this. The pages that have includes in them are to be given the .php extension and the includes themselves are to be .html pages? Right?

    Now, what happens if I have the include inside a <div> and that <div> has special CSS formatting? Will the .php page read the CSS properly? If not, how do I get the linked CSS information to the appropriate include?
    Cat-herder Extraordinaire

    Comment


    • #3
      Peg, well actually the file you include can be whatever extension you please.

      Hmm. Let's assume this is our div in index.php.
      HTML Code:
      <span style="color:#990000">
      <?php include('warning.inc'); />
      </div>
      .inc is just a popular extension for php includes when including text and the like. So now let's assume warning.inc looks like this....
      HTML Code:
      Abandon Ship!!
      When someone on the Internet requests your index page PHP fires up and outputs this to your server:
      HTML Code:
      <span style="color:#990000">
      Abandon Ship!!
      </div>
      Then your server outputs that to the person's browser. After the browser has it then it reads the CSS, it reads the text, and you now have something like Abandon Ship!!

      PHP plays no part on how the browser reads the page. This is because, as you can see, the browser never gets to see any PHP. You would get this same effect if you just statically wrote:
      HTML Code:
      <span style="color:#990000">
      Abandon Ship!!
      </div>
      on an .html page.

      Comment


      • #4
        Another question, I think. Some of my pages have anchor links to other pages. The link looks like this: <a href="page.php#section">Page Section</a>

        The PHP page has an include for the content. It reads: <?php include("content.html"); ?>

        By your logic, the anchor link should arrive at the correct anchor without a hitch, right?

        Peg
        Cat-herder Extraordinaire

        Comment


        • #5
          So long as the id "section" exists on the page, yes

          Comment


          • #6
            There's an <a name="section"> (or whatever the section name happens to be) on each place, yes.

            Okay, I think I've got it figured out. Thanks.

            Peg
            Cat-herder Extraordinaire

            Comment


            • #7
              Uh... yeah... only i just discovered if you're using dynamic includes based on user input, e.g.,
              PHP Code:
              <?php  
              include($_GET['page']);  
              ?>
              ... it's a potential disaster.

              Sample exploit:
              h ttp://yoursite.com/index.php?content=../../../../../insecure_php_scripts/shell_access.php

              The place i found that gave me a simple solution as so:
              PHP Code:
              if ( substr$_GET['content'] , ) != '.' // nothing starting with a period is allowed, preventing backing out of a directory.
              include( 'includes/' $_GET['content'] ); 
              ...and there's more on the subject here.

              /JoeyD goes off to fix a bunch of web pages...

              Comment


              • #8
                JoeyD, that is not a safe way to protect against this. I could easily start with a forward slash or easily enter a parent directory and return back to the child. The way to protect against this is to make sure the path begins with an expected root path.

                Comment


                • #9
                  That is EXACTLY what i use SSI for!

                  Comment


                  • #10
                    Originally posted by johnz View Post
                    PHP Code:
                    <?php include("yournavfile.html"); ?>
                    i always use a doc root
                    PHP Code:
                    <? include ($DOCUMENT_ROOT . '/your/nav/file.htm');?>
                    which is better? does it matter?

                    Comment


                    • #11
                      Okay, I have another silly question. This include works:

                      <?php include("header.php"); ?>

                      This does not:

                      <?php include("../header.php"); ?>

                      Why and what's my work around for it?
                      Cat-herder Extraordinaire

                      Comment


                      • #12
                        That is a valid path and should work, if the include is one directory up from the script calling it. Do you get a PHP error, complaining about not finding the file?

                        once suggested the following, i think as a means to help ensure that wherever the script goes, it can find its files...
                        PHP Code:
                        <?php
                        include dirname(__FILE__).'/../header.php';
                        ?>
                        ...the dirname(__FILE__) bit appends the full server path to the file, e.g. /home/myweb/public_html/test/../header.php, which i think means "descend into the /test directory and then look for header.php in its parent directory". I think.

                        I have no guess as to whether it's a secure thing to do or not, but telling someone to do the wrong thing is the fastest way to find out.

                        - joey

                        Comment


                        • #13
                          peg,
                          that syntax is correct. Are you certain that the file your referencing exists in the parent directory
                          "All I know is that I know nothing"

                          Comment


                          • #14
                            According to the online manual on php.net
                            If a path is defined (full or relative), the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
                            the link to the above
                            [ http://www.php.net/manual/en/function.include.php ]

                            the link to the 'include path'
                            http://www.php.net/manual/en/ini.cor...i.include-path

                            Comment


                            • #15
                              If you're asking why the ../ is necessary, Pegasus, it's because the header.php file is above the current file directory-wise. For example:

                              If you have header.php in your root directory, but your current file, say header.html is in a directory up (root-directory/this-directory/header.html) you would have to include a ../ when linking to header.php from the header.html file.

                              Confusing, right?

                              Comment

                              Working...
                              X