difficulties with include and writting to files

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm working on a very simple script that logs the ip address, time of
hit, and os/browser information and currently it works every time.  The
problem is getting the script included into my html document (so I can
log everyone that visits that page.)  Here is my code and how the files
are organized:

I know some of the code is probably sloppy, these are literally the
first php files i've written and I just started yesterday from
w3school's tutorial.



$br = new Browser;

$filename = 'hits.wordpad';
$today = date("n.j.Y H:i:s");

if (is_writable($filename)){
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";

    /* Write the following:
        IP Address[newline]
        Operating System[space]Browser[space]Browser Version[newline]
      if (fwrite($handle, $today) && fwrite($handle, "\n") &&
fwrite($handle, $_SERVER["REMOTE_ADDR"]) && fwrite($handle, "\n") &&
fwrite($handle, $br->Platform) && fwrite($handle, " ") &&
fwrite($handle, $br->Name) && fwrite($handle, " ") && fwrite($handle,
$br->Version) && fwrite($handle, "\n") && fwrite($handle, "\n") ==
else {
   echo "The file $filename is not writable";


$path = './php/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
include ('hitcounter.php');


If I open my browser and directly go to
http://.../root/php/hitcounter.php everything works fine.  The file
gets appended with my IP and browser information as it should.  If I go
to http://.../root/includetest.php I get an error output "The file
hits.wordpad is not writable".  This is odd because that particular
error message only appears in hitcounter.php ... so I know that that
code is executing.  But yet, that code never has a problem executing
when I visit the php file directly.

Lastly, assuming someone can get this solved for me, my next question
is about including the hitcounter in an html document.  I was thinking
the following would work:
include 'php/hitcounter.php'
However, when I tried this with a similar script, it appears to do
nothing and I can view the page source of my html file and see the php
code (shouldn't the server replace that block before sending the html
file out?)

I have checked file and folder permissions on my server (as I have had
problems with write protected files) but can't seem to figure this one
out.  I have looked at all the resources I know of and run many
searches trying to solve this without wasting you guy's time, but alas,
I come for help.

Thanks in advance,

Re: difficulties with include and writting to files

Quoted text here. Click to load it

The problem here is most likely the path to the file. Let's say the file you
want to write to is in


If you are trying to write to this file from a file in /root/ then the path
is simply temp/file.txt.

However, if you are in /root/php/, the path is ../temp/file.txt

To get around this problem, take a look at
as well as dirname and a few other related functions

Quoted text here. Click to load it

IIS or Apache?

Re: difficulties with include and writting to files

Well I figured it out while I was working on another php script.  I can
best describe my problem with an examle.


Goal: have the date that index.php was modified added to the page.

index.php 's contents:
include 'php/hitcounter.php';

date_modified 's contents:
$filename = 'index.php';
if (file_exists($filename)) {
    echo "Page last modified: " . date ("F d Y H:i:s",
filemtime($filename)) . " EST";

Now, since date_modified is in a folder ('php') and index.php is in the
root directory, I would have thought that $filename = 'index.php'
should have been $filename ='../index.php', HOWEVER, since the way I am
executing date_modified.php is by including it into index.php, I do not
need to move up yet another directory.

To see this script live you can check out
http://www.cse.msu.edu/~meyerro3 although its nothing fancy, it just
says the date I last modified index.php at the bottom.

Anyways, now I have a new question.  Is there another way I can call a
php script to execute other than including it's contents into my
current document?  Ideally, I would like to be able to pass over a
variable to date_modified saying which file to check (so I can use it
for more than just my index page.)  I like having all my php scripts
organized in a folder set inside root, however, using include() skews
realative pathnames.  What if I want to include date_modified into a
page that is located in root/archived_news/whatever.php?

Lastly, and sorry for the essay-sized post, my previous problem with
hitcounter.php has been solved.  My problem was that my index page was
.html so none of the php was being executed.  Simply renaming it to
.php did the trick, although I have also read that I can play around
with some server-side settings to scan .html files as well.


Site Timeline