1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to insert header/footer server side for existing html files

Discussion in 'Programming' started by jjdubb, Mar 23, 2004.

  1. #1
    I have existing html files that I would like to insert headers and footers onto. There are too may to do by hand. I would like to do some kind of search and replace to add the the code in the correct place server side. What's the best way to do this? Preferably in php or perl. Thanks.
     
    jjdubb, Mar 23, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Do you want to insert the header/footer statically (so a copy of it exists within each file), or have the files include a "master" header/footer file?

    - Shawn
     
    digitalpoint, Mar 23, 2004 IP
  3. Foxy

    Foxy Chief Natural Foodie

    Messages:
    1,614
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I would like to know this too and is it possible to create a dynamic site map? :)
     
    Foxy, Mar 24, 2004 IP
  4. Mr T

    Mr T Guest

    Messages:
    62
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    With PHP, you can just do
    include 'header.php';
    PHP:
    Then put in header.php all the html you want to go at the start of each page.

    Foxyweb, apparently is it possible to create a dynamic sitemap, but it really depends on your site as to how well it works.
     
    Mr T, Mar 24, 2004 IP
  5. Foxy

    Foxy Chief Natural Foodie

    Messages:
    1,614
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you tell me more on the site map question?

    For instance what sort of site does it work well on?

    Thanks
     
    Foxy, Mar 24, 2004 IP
  6. Mr T

    Mr T Guest

    Messages:
    62
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry, I dont really know much more, just read it on a forum somwhere.

    I'm sure hotscripts.com could furnish you with some auto sitemap code, and maybe even a tutorial.
     
    Mr T, Mar 24, 2004 IP
  7. jjdubb

    jjdubb Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I would like the header/footer to be inserted server side as the file is requested. I know I could manually add an include statement where needed in each file. I would prefer to have the code automatically inserted so that I don't really have to do anything else.

    An example of what I want to accomplish is basically what the free hosting sites do with advertising headers/footers.

    When you upload an html page to the free hosting site - they add an advertising header and footer server side without tampering with the rest of the page's html.
     
    jjdubb, Mar 24, 2004 IP
  8. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #8
    The best thing to do (that I can think of) would not be to alter each individual file, but use mod_rewrite to pass everything through a simple PHP script (or whatever scripting language you like) that does it for you on the fly.

    - Shawn
     
    digitalpoint, Mar 24, 2004 IP
  9. jjdubb

    jjdubb Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    So, how would I setup a mod rewrite to insert a header/footer on every html file?
     
    jjdubb, Mar 24, 2004 IP
  10. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #10
    Well first things first... are you using an apache web server with mod_rewrite enabled?

    - Shawn
     
    digitalpoint, Mar 24, 2004 IP
  11. jjdubb

    jjdubb Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yes, I have apache with mod rewrite available.
     
    jjdubb, Mar 24, 2004 IP
  12. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #12
    You could have your .htaccess file setup like so in the root of the website:

    <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !passthru.php
            RewriteRule ^(.*)       /passthru.php?file=$1
    </IfModule>
    Code (markup):
    Have passthru.php in the root of your web directory as well...

    <?php
    	readfile("header.inc");
    	readfile($file);
    	readfile("footer.inc");
    ?>
    PHP:
    header.inc and footer.inc also in the same folder (those of course are your header/footer files).

    Then any request for a file (regardless of directory) within the site will have header.inc added to the beginning and footer.inc added to the end.

    You could get trick within the passthru.php file and do a little better insertion (for example insert the header right after the <BODY> tag for example). But that should get you started...

    Ultimately the best thing to do would be to make the footer.inc file link to www.digitalpoint.com though. hahaha

    - Shawn
     
    digitalpoint, Mar 24, 2004 IP
  13. jjdubb

    jjdubb Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks, that's exactly what I'm looking for. Do you know the php insert command right off the top of your head?
     
    jjdubb, Mar 24, 2004 IP
  14. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #14
    What insert command?

    - Shawn
     
    digitalpoint, Mar 24, 2004 IP
  15. jjdubb

    jjdubb Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    The command to search for the a particular tag on the existing html file that is being passed through and then insert the the header code.
     
    jjdubb, Mar 24, 2004 IP
  16. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #16
    Oh... there isn't a single command for that... you basically would need to read in the file, evaluate it and do some manipulation of the text instead of just using the readfile() command (which passes it right through).
     
    digitalpoint, Mar 24, 2004 IP
  17. jjdubb

    jjdubb Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Cool, I'm going to go to work on the script then. Thanks a lot!
     
    jjdubb, Mar 24, 2004 IP
  18. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Hey guys.
    Stop! The digital point script is a bit dangerous!! You should make sanity checks before opening the file or your passwords......

    http://host.com/passthru.php?file=../../../../etc/xxx

    PHP has a directive to append files automatically. Check your php.ini file:
    "
    ; Automatically add files before or after any PHP document.
    auto_prepend_file =
    auto_append_file =
    "

    you may also set this in a .htaccess file.
     
    nlopes, Apr 3, 2004 IP
  19. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #19
    Wouldn't work of course for non-PHP files... but you could setup Apache to run any file extension through the PHP parser.

    - Shawn
     
    digitalpoint, Apr 3, 2004 IP
    nlopes likes this.
  20. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Yes, you may configure apache to parse .html files.
     
    nlopes, Apr 3, 2004 IP