5
votes

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.

5
What errors are you seeing? Check the web server error log. If it works on one server but not another, your PHP itself should be fine - but paths or permissions are different.helion3
@BotskoNet - I have checked the error logs of the website, and have confirmed that there are no errors. This was the first suggestion that the technical staff of the web hosting providers made.Bill
tried using index.php vs index.html?Ryan
@Ryan - Yes, I have also tried this, but nothing happened. As a sanity check, I will try to do this again, just in case I got it wrong....Bill
When PHP fails to include a file it throws an error, unless you have PHP configured to ignore that error level. I would suggest running a debug statement to print the full filepath being used in the include, and use console or something to verify the file exists at that location.helion3

5 Answers

6
votes

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.

1
votes

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'; ?>

0
votes

sometimes it might be dues to casing. check if you you uppercase in naming. on some servers Index.php is not equal to index.php

-1
votes

You might try pasting the "include" code directly into the calling code. Maybe it's the included code itself that's misbehaving...

-2
votes

You are doing completely incorrect thing in the first place.

PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html

is just totally wrong

There is no such thing as embedding php file within html file (aside from mod_rewrite ...). For PHP script to be interpreted you must have it (in 99% of cases) with php suffix. This way you allow PHP to distinguish it from regular PHP and send to php interpreter.

Put simply - create this file (a.html):

<body>
abcd<?php echo 'efgh';?>
</body>

and see the result in your browser - use .../a.html

What do you see?

abcd

and not

abcdefgh

On top you always have to have php not the other way around. Solve this or update your question if incorrect.