Why PHP include is working on local server and not on website

14,779

Solution 1

Do you get any PHP error?

First of all, you need to activate error reporting.

Put this before including your file

ini_set('display_errors',1);
error_reporting(-1);

PHP should tell you what's happening.

If you don't see anything, change the filename index.html to index.php and try again.

Solution 2

Maybe be you have used "\" in your include path

Wrong:

<?php include 'includes\header.php'; ?>

You should use "/" to work.

Current:

<?php include 'includes/header.php'; ?>

Share:
14,779
Bill
Author by

Bill

Bill has just completed his Masters by Research in Computer Science and Software Engineering at the University of Wollongong. His research topic was in Lattice Based Cryptography. Bill is currently working as a sub-contractor for small companies and organisations developing desktop and/or web-based applications.

Updated on June 12, 2022

Comments

  • Bill
    Bill almost 2 years

    Problem Description in Brief:
    PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html file, but does not work when I upload it to my website. Note that I have made sure that all paths are correct, and that the script file has its own php tags, etc.

    Problem Description in Detail:
    Yes, I am new to PHP scripting, and yes, variants of this question have probably been asked before. The answers to a few of the questions I have read have noted the path of the php script files to be incorrect. I have checked all paths and confirmed that they are indeed correct (including those on the web hosting server). Furthermore, I have been successful in getting the script to work on my local server running Apache2 with PHP5, but have not been successful when uploading it to my website.

    Essentially, I am trying to implement a hit counter script which I have acquired from a Stack Overflow post labelled Visitors counter for simple web sites like Vinaora. The code that invokes the php script looks something like this....

      &ltfooter&gt
        &lt!-- Execute Hit Counter Script --&gt
        &lt?php include($_SERVER['DOCUMENT_ROOT'].'/php/hitcounter.php'); ?&gt
      &lt/footer&gt
    

    For the likes of me, I cannot figure out why it does not work on the web hosting server. I have tried other combinations of invoking the script like,

      &ltfooter&gt
        &lt!-- Execute Hit Counter Script --&gt
        &lt?php include('./php/hitcounter.php'); ?&gt
      &lt/footer&gt
    

    and,

      &ltfooter&gt
        &lt!-- Execute Hit Counter Script --&gt
        &lt?php include(dirname(__FILE__).'/php/hitcounter.php'); ?&gt
      &lt/footer&gt
    

    All combinations seem to work on my local web server, but not on the website! Also note that, I have no problem invoking other PHP scripts using other methods (even on the web hosting server), eg.

        &ltform id="form-query" onsubmit="this.checkValidity();" action="./php/contact.php" method="post"&gt
    

    Any advice/suggestions would be appreciated.

  • Bill
    Bill over 10 years
    Thanks Tivie, changing the filename index.html to index.php has solved my problem! Didn't have to enable reporting, although as a matter of course, and for future reference, I will do so. Thanks, once again.
  • Bill
    Bill over 10 years
    The question is correct. Apparently, it is possible to 'include' a php script from an html file if you configure the web server to do so by adding the line <AddType application/x-httpd-php .html> in the /etc/apache2/httpd.conf file. This is what I had done, and why it worked for me, and not my web hosting server.
  • Filip Kováč
    Filip Kováč almost 6 years
    you won't believe me but really this was the problem I had ... it couldn't find file with "\" ... just on server