difficulties with include and writting to files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robizzle

    difficulties with include and writting to files

    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.

    root/index.html
    root/php/hitcounter.php
    root/php/hits.wordpad
    root/php/browser.php
    root/includetest.php

    hitcounter.php:
    <?php

    require_once('b rowser.php');
    $br = new Browser;

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

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

    /* Write the following:
    Month.Day.Year[space]Hour:Minute:Sec ond[newline]
    IP Address[newline]
    Operating System[space]Browser[space]Browser Version[newline]
    */
    if (fwrite($handle , $today) && fwrite($handle, "\n") &&
    fwrite($handle, $_SERVER["REMOTE_ADD R"]) && 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") ==
    FALSE){
    exit;
    }
    fclose($handle) ;
    }
    else {
    echo "The file $filename is not writable";
    }
    ?>

    includetest.php :
    <?php

    $path = './php/';
    set_include_pat h(get_include_p ath() . PATH_SEPARATOR . $path);
    include ('hitcounter.ph p');

    ?>

    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:
    <?php
    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,
    Rob

  • Mark Rees

    #2
    Re: difficulties with include and writting to files

    > root/index.html[color=blue]
    > root/php/hitcounter.php
    > root/php/hits.wordpad
    > root/php/browser.php
    > root/includetest.php
    >
    > hitcounter.php:
    > <?php
    >
    > require_once('b rowser.php');
    > $br = new Browser;
    >
    > $filename = 'hits.wordpad';[/color]
    ....[color=blue]
    > ?>
    >
    > includetest.php :
    > <?php
    >
    > $path = './php/';
    > set_include_pat h(get_include_p ath() . PATH_SEPARATOR . $path);
    > include ('hitcounter.ph p');
    >
    > ?>
    >
    > 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.[/color]

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

    /root/temp/

    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
    [color=blue]
    >
    > 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:
    > <?php
    > 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?)[/color]

    IIS or Apache?



    Comment

    • Robizzle

      #3
      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.

      root/index.php
      root/php/date_modified.p hp

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

      index.php 's contents:
      <?php
      include 'php/hitcounter.php' ;
      ?>

      date_modified 's contents:
      <?php
      $filename = 'index.php';
      if (file_exists($f ilename)) {
      echo "Page last modified: " . date ("F d Y H:i:s",
      filemtime($file name)) . " 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.p hp 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.

      Cheers,
      -Rob

      Comment

      Working...