Simple PHP Include Problem

by alt55
10 replies
The last few days Ive spent trying to get a php include to work on the index page of my site.

I have a folder with a php file that Im trying to call but it does not show up. Hosting support cannot help me with code, so Ill ask here.

Ive tried it with all sorts of dots and forward slashes combinations but the php include does not work. My site is an HTML site, maybe thats a problem but I was told this is perfectly OK.

This is just to show a snippet of text on the index page of my site designerrugs dot co

I have the code in the footer of the index page and when I view the source code, It states an error that Im using an xml file html. I dont understand this

I think this is the right path for the index page - I think
<?php include("./directory/file.php") ?>

Any sort of help would be much appreciated
Regards A
#include #php #problem #simple
  • Profile picture of the author Brandon Tanner
    I'm not sure if it's the cause of your problem, but you shouldn't have parenthesis around your include path. And you shouldn't have a single dot at the beginning of the include path either.

    If you want to start the include path from the current directory, it should look like this...

    <?php include "directory/file.php"; ?>

    If you want to start it from one directory up, then it should look like this...

    <?php include "../directory/file.php"; ?>

    That said, if all you want to do is have PHP display "Copyright" and the current year, then it would be simpler just to do this instead...

    <?php echo " Copyright " . date("Y"); ?>
    Signature

    {{ DiscussionBoard.errors[6347167].message }}
    • Profile picture of the author alt55
      Thanks for your reply Brendan T. Yes you are right - I have taken away the parenthesis but I still cannot get it working. My hosting service will not help me on this as it involves code. I will use your other Copyright code
      Regards Alan..
      {{ DiscussionBoard.errors[6347237].message }}
  • Profile picture of the author failideas
    is your site really html?
    do you use some kind of cms?
    Signature
    Dental WordPress Theme - Dentist WP Theme - Lawyer WordPress Theme

    ++ Theme Release News and Promotions > submit here
    Affiliates - wanna make good money. Subscribe here Affiliates List
    {{ DiscussionBoard.errors[6347366].message }}
    • Profile picture of the author Brandon Tanner
      I just noticed that your home page is an html file. That won't work since you are using php code in the page. That page needs to be index.php instead of index.html (and all other pages that contain php code need to have a .php extension as well).

      If you'd rather not go to the trouble of changing all of your pages to .php (which would also involve changing all of the links that point to those pages), an easier solution would be to simply use Javascript to display the current year...

      <script type="text/javascript">
      var theDate=new Date()
      document.write(theDate.getFullYear())
      </script>
      Signature

      {{ DiscussionBoard.errors[6347441].message }}
    • Profile picture of the author alt55
      Originally Posted by failideas View Post

      is your site really html?
      do you use some kind of cms?

      Yes I use simple HTML and CSS by hand. Wish I hadn't bothered now that I know a little of the power of PHP, especially for keeping large menus etc....
      {{ DiscussionBoard.errors[6348034].message }}
      • Profile picture of the author failideas
        Originally Posted by alt55 View Post

        Yes I use simple HTML and CSS by hand. Wish I hadn't bothered now that I know a little of the power of PHP, especially for keeping large menus etc....
        You could rename your pages to .php
        and use an html/text editor to replace all .html text inside files of your site folder for .php and you are all set.

        doing these 2 steps will make your site work without problems and the code you want to include will work.
        Signature
        Dental WordPress Theme - Dentist WP Theme - Lawyer WordPress Theme

        ++ Theme Release News and Promotions > submit here
        Affiliates - wanna make good money. Subscribe here Affiliates List
        {{ DiscussionBoard.errors[6348528].message }}
  • Profile picture of the author Charles AK
    <?php include("./directory/file.php"); ?>


    your missing a ;

    that should work.
    {{ DiscussionBoard.errors[6347491].message }}
    • Profile picture of the author wayfarer
      Originally Posted by Charles Akers View Post

      <?php include("./directory/file.php"); ?>


      your missing a ;

      that should work.
      The last semi-colon is optional, as long as it's right before the closing ?> php tag. Yet another PHP quirk...
      Signature
      I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
      {{ DiscussionBoard.errors[6347517].message }}
  • Profile picture of the author alt55
    Do you think this line of code in the .htaccess file would help?
    AddType application/x-httpd-php .php .html
    {{ DiscussionBoard.errors[6347950].message }}
    • Profile picture of the author wayfarer
      Originally Posted by alt55 View Post

      Do you think this line of code in the .htaccess file would help?
      AddType application/x-httpd-php .php .html
      Yes, that may work, depending on how your server is configured however. Or else, just change your index.html to index.php. I went to the site you indicated in your first post and "viewed source". I saw the <?php ?> tags show up right in the source code that is being processed by the browser. PHP will never show up in the source code if it is being processed.

      Browsers, when presented with short lines of <?php ?> code, will not display it, since it is not a valid HTML tag. The problem is your code just isn't being processed by the server before it is sent to the browser.
      Signature
      I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
      {{ DiscussionBoard.errors[6348243].message }}

Trending Topics