473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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('browser.php');
$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)";
exit;
}

/* Write the following:
Month.Day.Year[space]Hour:Minute:Second[newline]
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") ==
FALSE){
exit;
}
fclose($handle);
}
else {
echo "The file $filename is not writable";
}
?>

includetest.php:
<?php

$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:
<?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

Oct 5 '05 #1
2 1601
> root/index.html
root/php/hitcounter.php
root/php/hits.wordpad
root/php/browser.php
root/includetest.php

hitcounter.php:
<?php

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

$filename = 'hits.wordpad'; .... ?>

includetest.php:
<?php

$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.
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
http://uk2.php.net/manual/en/function.realpath.php
as well as dirname and a few other related functions

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?)


IIS or Apache?

Oct 5 '05 #2
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.php

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($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.

Cheers,
-Rob

Oct 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yawnmoth | last post by:
i'm trying to write a php script that will password protect some random directory by creating a .htaccess file, and a password file to accompany the .htaccess file, and it isn't working... ...
2
by: brolewis | last post by:
I am trying to deploy Python onto a number of laptops and have been trying to take advantage of Python 2.4's MSI installer. I have tried using the following commands to install, but to no avail: ...
5
by: hijwel | last post by:
<script language="JavaScript"> <!-- document.write('<form name="combo"><select name="example" size="1 onChange="Draw()">'); document.write('<option value=none>Maak je keuze');...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
1
by: ya man | last post by:
when i use #include <iostream.h> in some files i get lots of error messages of the kind 'ambiguous symbol this is solved when i use #include <iostream why is that ? and can i use #include...
0
by: Hai Nguyen | last post by:
Hi everyone I'm writting a javascript for my validators. Some of them will be validated or server side, some of them will be validated on client side. Since I don't want to mix my javascript...
1
by: itsjyotika | last post by:
Hello Everyone, I need to read data from a CVS file(i created it from micosoft excel) and then need to match it with the one of the date from the command line.If the date is there then it should say...
0
by: marinhof | last post by:
hi i need some guidance.. this is the situation im working on web app using Visual studio 2003 working with C# and asp.net the database is managed by sql studio managmnet 2005.. the thing is i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.