I am able to do PHP includes, but am having difficulty in doing simple html include files. Here's what I've been doing:

PHP includes-

<?
include("file.inc");
?>

This works for me, although for some reason I have to put <?...?> in the include file.

My real problem is what the actual syntax for an html include file?

This is what I've been typing:

<!--include("file.inc")-->

It comes up with all sorts of errors. I've had it right in the past but my memory has escaped me and I can't get such a simple thing to work. Why doesn't the HTML include work properly?

    HTML includes work the same way as PHP includes:

    include('someFile.html');
    include('anotherFile.php');
    

    And of course any PHP code which might exist in an include file must be 'seperated' from the text/html using the PHP open and close tags:

    <?php ... ?>
    

      Hi,

      You're talking about using SSI (Server Side Includes) rather than PHP. The correct syntax is:

      <!--#include virtual="path_to_file.ext" -->

      Best wishes,
      Simon.

        Yes, thats what I'm looking for. I don't think you can have the other solution that was said.

        <body>
        include("file.html");
        </body>

        That won't work. Anyways, the part that is in the include file wouldn't be a full html file. It would be part of one. So SSI is the way to go I think, I'm testing it out right now as we speak. Looks familiar too.

          I typed it verbatim and it doesn't work. I then cut and pasted the external file back into the code and now it works. But I want to use include files. Does SSI need to be installed as an extra? My host should have it shouldn't they? The php include method will work except I know how finnicky PHP is with white space and I have a large chunk of code to put in the external file. This is the amount of code I want to include:

          <!-- Beginning of Layers-->
          <div id="loginWin" class="drag">
          <TABLE WIDTH=250 height="337" BORDER=0 CELLPADDING=0 CELLSPACING=0>
          <TR>

          <TD COLSPAN=3 width="250" height="44"><IMG SRC="images/sign-in_01.jpg" WIDTH=250 HEIGHT=44></TD>
          </TR>
          <TR>

          <TD WIDTH="43" HEIGHT="293" ROWSPAN=2><IMG SRC="images/sign-in_02.jpg" WIDTH="43" HEIGHT="293"></TD>
          <TD width="163" height="248" valign="top" bgcolor="#cec59b">
          <form action="<?php echo$PHP_SELF;?>" method="post" name="checkLogin">
          <p>
          <font color="#504c42" size="2" face="Arial, Helvetica, sans-serif">
          <strong><br>Username: </strong>
          </font>
          <input name="userID" type="text" id="userID" size="22">
          <br><br>
          <strong>
          <font color="#504c42" size="2" face="Arial, Helvetica, sans-serif">Password:</font>
          </strong>
          <input name="passWord" type="password" id="passWord" size="22">
          </p>
          <p align="center">
          <input type="submit" name="Submit" value="Submit">
          &nbsp;
          <input type="reset" name="Submit2" value="Reset">
          </p>
          </form>
          <p align="center"><br><br>
          <img src="images/closeWindow.jpg" width="70" height="17" border="0" usemap="#close">
          </p>
          </TD>
          <TD ROWSPAN=2 WIDTH=44 HEIGHT=293><IMG SRC="images/sign-in_04.jpg" WIDTH=44 HEIGHT=293></TD>
          </TR>
          <TR>
          <TD WIDTH=163 HEIGHT=45><IMG SRC="images/sign-in_05.jpg" WIDTH=163 HEIGHT=45></TD>
          </TR>
          </TABLE>

          <map name="close" id="close">
          <area shape="rect" coords="-1,0,70,18" href="javascript:showLayer('loginWin');">
          </map>
          </div>
          <!-- End of layer -->

          I'm gonna use it in several other files, so it makes sense to use includes. What do I do?

            Hi,

            Did you remember to change the file extension to .shtm or .shtml (your main html file, not the include file)? Otherwise most servers won't parse any SSI directives in the file.

            Best wishes,
            Simon.

              It has PHP in the file so I guess calling it .shtml would be out of the question, hey? I guess I'll have to figure out how to use a php include to do the trick. Thats all there is to it then. Unless anybody else has a better idea?

                Hi,

                I guess I must be missing something because I just ran a test here and it worked fine. Here's what I did:

                Main file - test.shtml

                <html>
                <head>
                <title>SSI Test</title>
                </head>
                <body>
                <!--#include virtual="stuff.php"-->
                </body>
                </html>

                Include file - stuff.php

                <div align="left">
                <table border="1" cellpadding="0" cellspacing="0">
                <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                </tr>
                <tr>
                <td>&nbsp;</td>
                <td><?php echo "PHP code went here";php?></td>
                </tr>
                </table>
                </div>

                You can see the result at http://www.aqhost.com/test.shtml

                Isn't this what you want to achieve? If not, please break it down into which file contains which type of code.

                Best wishes,
                Simon.

                  They work but there are some flaws. I have a file called css2.shtml and inside that file I call for several other scripts. One called connectDB.php (function that when called will connect you to the database), another is called authentication.php (checks your form and compares it with the database and if valid will redirect you to the admin section of the site), the last script is called login.php (will display a form on a layer that will pop-up when a button is pressed, on the form you must login). In login.php most of it is html except for one part that is the form action, it says:

                  <?php echo $PHP_SELF;?>

                  Because it is the form action it traces the path to the include file and sends the variables there. Not good, what happens is that the page redirects and the layer is now displaying on its own page seperate from the page that popped it up. I've tried hardcoding the link in to css2.shtml but that doesn't work either. Maybe I can't use includes for what I want to do? To test the code go to www.shabangweb.com/css2.shtml. The username and password to try is rdykes and chew. If you want to see any of the code let me know and I will display it for you. It all works properly when its all together. Its when I seperate it all that it messes up.

                    At first glance it appears from your description that the form data should be sent to authentication.php, right? Otherwise you're in a loop where login.php gets shown over and over again. If I'm right then you should just hardcode the form action within login.php to http://www.shabangweb.com/authentication.php or whatever.

                    If that still doesn't work, email me the code to simon 'at' aqhost.com and I'll take a closer look.

                    Best wishes,
                    Simon.

                      Give me 2 or 3 minutes and I'll show you what happens.

                        You get this error:

                        Fatal error: Call to undefined function: db_connect() in /home/shabangw/public_html/authentication.php on line 4

                        I can't show you the code because it has my database login. But basically I have a file that is a function to connect to the database the other calls that function thats why its saying where the heck is the function. Its in the other include file. I wanted to keep them seperate so I could connect in different ways in the future. If there's no other way I'll combine the two but it should still work, shouldn't it?

                          What if you put:

                          require("/full/path/to/connectDB.php");

                          At the top of authentication.php? That way the function will be included but you can still keep it separate.

                          Simon.

                            Good idea I hadn't thought of it but I get an error again.

                            Fatal error: Failed opening required '/full/path/to/connectDB.php' (include_path='') in /home/shabangw/public_html/authentication.php on line 2

                            I hope you like helping people debug code?

                              You didn't literally put /full/path/to/connectDB.php did you? You need to replace /full/path/to/ with the actual path e.g. /home/shabangw/public_html/

                              And yes, I love debugging code 😃

                              Simon.

                                I didn't put the path though as they are at the same level in the hierarchy. The path didn't work honestly /home/shabangw/public_html/. Don't know why but I guess it doesn't matter. thanks for the help, I actually learned a lot from you. I had no idea what .shtml was before today but now i know it has to do with include files. I wonder if it has any other function? As well the require code in php is good to know. I'm sure I'll use that a lot now. I'm learning. Thanks. 🙂

                                  Yay! Glad it works now. SSI has a whole load of functionality besides including files. There's a good tutorial on how to use it here

                                  Best wishes,
                                  Simon.

                                    Write a Reply...