Inserting php script in html? How the hell do I do it?

Discussion in 'OT Technology' started by Silent, Jun 2, 2003.

  1. Silent

    Silent New Member

    Joined:
    Sep 20, 2001
    Messages:
    1,831
    Likes Received:
    0
    Location:
    Chicagoland
    You guys probably know this off the top of your heads, but I'm stumped... I have a guestbook written in PHP located HERE .

    I would like to put that php script inside one of my existing html pages so that I can still keep the look of my website going, but I have no idea how to go about doing it. Is there just a simple command that I can use to do this?
     
  2. druid

    druid What do the 5 fingers say?

    Joined:
    May 13, 2001
    Messages:
    702
    Likes Received:
    0
    <?php inclue("guestbook.php"); ?>

    Should do the trick, you will however probably have to rename the html page you want to include it on to .php
     
  3. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,079
    Likes Received:
    281
    Location:
    nw iowa
    also:

    <?php require("questbook.php"); ?>

    will work.......... i think 'insert' does as well... theres a few ways.
     
  4. Silent

    Silent New Member

    Joined:
    Sep 20, 2001
    Messages:
    1,831
    Likes Received:
    0
    Location:
    Chicagoland
    Whenever I try to do

    <?php require("guestbook/guestbook.php"); ?>

    I get the following: http://www.lswon.com/pictures.php

    (this is a test page) I'm just trying to get the guestbook to work on here for now :) I get something similar when I try to do include instead of require. :dunno:
     
  5. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Assuming the errors you're referring to are still the same:

    Code:
    Warning: Cannot send session cookie - headers already sent by (output started at /home/.tabathawasher/lswon/lswon.com/pictures.php:7) in /home/.tabathawasher/lswon/lswon.com/guestbook/guestbook.php on line 21
    
    
    Its going to be a bit more difficult to just do

    PHP:
    include('guestbook/guestbook.php');
    // or
    // require('guestbook/guestbook.php');
    The error you're getting is because PHP is trying to send header info to the browser, but the HTTP rules dictate that header information comes before any web page data (so header info -> then web page).

    In this case, you're sending your HTML out, then I bet guestbook.php does some tricks with cookies or sessions. Well, those tricks won't work if you're already dumping out HTML to the browser.

    (an interesting side note: this is one of the few times that IIS/ASP has a brief advantage over PHP - but only for the novice scripter)

    Here's the quick and dirty way that should solve your problem:

    PHP:
    ob_start();   // put this at the top of your PHP page 
    ob_start() tells PHP to do all the work of processing your script, but it won't send it to the browser right away. Once its done processing, it then puts it in a browser friendly order. So now all the header info gets sent out first - which is how things should be.

    (another interesting side note: this ob_start() is transparent in IIS/ASP - meaning IIS/ASP does this for you automatically)

    Give that a whirl...
     
  6. Silent

    Silent New Member

    Joined:
    Sep 20, 2001
    Messages:
    1,831
    Likes Received:
    0
    Location:
    Chicagoland
    I think the best thing I could do is redo my whole site so that it all uses php. Right now I have it setup as a template in dreamweaver, and whenever I need a new page, well its easy, but I'm thinking it would be easier if everything was in php since I'm using more and more scripts.

    I should pick up a book and learn more about php. :(
     
  7. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Depending on how creative you want to get with PHP, you can templatize your site using PHP and really make your life easy when you need a new page. A super quick and easy way to do it:

    PHP:
    <?
    ShowHeader('Page Title Could Go Hear');
    ?>
    You're page content could go here as well as any HTML you want
    <?
    ShowFooter();
    ?>
    Then when its time for a new page, just copy or insert the ShowHeader/Footer functions and you're good to go.

    These two functions could be as simple as just the HTML code wrapped around by these two functions - make sense?

    So ShowHeader() might look like:
    PHP:
    function ShowHeader($title = 'My Title') // default $title so you can leave it blank if you want - if you do, then 'My Title' will be shown
    {
    ?>
    <html>
    <head>
    <title><?=$title?></title>
    </head>
    <body bgcolor="#FFFFFF">
    <?
    }  // end function ShowHeader(...

    // now for the footer
    function ShowFooter()
    {
    ?>
    </body>
    </html>
    <?
    }  // end function ShowFooter()
    This is very very very basic. Hopefully this sparks some ideas. If you REALLY want to get fancy, you can setup file templates - so you have PHP code read the template from a file and then even have it do some parsing so your variables get dropped in for you (this is something I've moved to and its allowed me to prototype entire websites in no time - just change the template files and you're good to go).
     
  8. Silent

    Silent New Member

    Joined:
    Sep 20, 2001
    Messages:
    1,831
    Likes Received:
    0
    Location:
    Chicagoland
    I have an idea of how to do the header & footer, but I don't want to change the layout of my site one bit... would it be possible to have that same menu on the left hand side? as well as everything else remain the same?

    Man I wish I had someone showing me how to do this shit... I would get a hang of it quick :(
     
  9. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Yes, you can leave your menu on the left (I've done it several times - http://www.teamtekro.com is a good example - although its due for a site redesign within the next few weeks). TeamTekro.com actually uses an approach very similar to what you're looking to do. Header, footer, and then nav menus all combined via PHP. For teamtekro.com, I've defined a column for my menus to sit in and then I toss in some PHP code to build the menus themselves (there's dynamic nav menus there and I have several web apps on that site that create their own nav menus on the fly).

    Its not difficult. You just have to take one step at a time and test often.
     

Share This Page