bubu
January 19th, 2004, 12:04 PM
I'm newbie in PHP and I am coding a simple text counter. I'm using PHP version 4.3.4 in Xitami 2.5b4 and WinXP Pro.
When I execute the php file, this is the output:./kounterfiles/~hitcounter~index.php.txt
Warning: fwrite(): supplied argument is not a valid stream resource in D:\WebDevelop\local\hitcounter\index.php on line 29
Warning: fclose(): supplied argument is not a valid stream resource in D:\WebDevelop\local\hitcounter\index.php on line 30
Hit Counter: Error opening file for reading.
Note: The "Error opening file for reading" is an error handler that I made due to the fact that it will overload ~350mb of RAM mem if it is not there. And also, it seems to be hiding the same error shown above it ("supplied argument is not a valid stream resource")...
Here is the code:
<?php
/*
Based on php.inc.ru's counter
Dev by BubuX (BubuX@phreaker.net)
*/
// Config settings here
$file_dir = "./kounterfiles/";
$file_ext = ".txt";
$currfile = $file_dir . str_replace('/','~',$_SERVER['SCRIPT_NAME']) . $file_ext;
echo $currfile;
// Log visit
function log_visit() {
$fp = fopen($currfile, "a");
//if (!$fp) {
//echo "<br>Hit Counter: Error opening file for appending.<br>";
//}
//else {
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\13\10";
fwrite($fp, $line);
fclose($fp);
//}
}
// Show visit count
function show_visit() {
$fp = fopen ($currfile, "r");
if (!$fp) {
echo "<br>Hit Counter: Error opening file for reading.<br>";
}
else {
$visits = 0;
while (!feof ($fp)) {
$line = fgets($fp, 4096);
$visits++;
//echo $buffer;
}
fclose ($fp);
// Display visits count
echo $visits;
}
}
log_visit();
show_visit();
?>
When I execute the php file, this is the output:./kounterfiles/~hitcounter~index.php.txt
Warning: fwrite(): supplied argument is not a valid stream resource in D:\WebDevelop\local\hitcounter\index.php on line 29
Warning: fclose(): supplied argument is not a valid stream resource in D:\WebDevelop\local\hitcounter\index.php on line 30
Hit Counter: Error opening file for reading.
Note: The "Error opening file for reading" is an error handler that I made due to the fact that it will overload ~350mb of RAM mem if it is not there. And also, it seems to be hiding the same error shown above it ("supplied argument is not a valid stream resource")...
Here is the code:
<?php
/*
Based on php.inc.ru's counter
Dev by BubuX (BubuX@phreaker.net)
*/
// Config settings here
$file_dir = "./kounterfiles/";
$file_ext = ".txt";
$currfile = $file_dir . str_replace('/','~',$_SERVER['SCRIPT_NAME']) . $file_ext;
echo $currfile;
// Log visit
function log_visit() {
$fp = fopen($currfile, "a");
//if (!$fp) {
//echo "<br>Hit Counter: Error opening file for appending.<br>";
//}
//else {
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\13\10";
fwrite($fp, $line);
fclose($fp);
//}
}
// Show visit count
function show_visit() {
$fp = fopen ($currfile, "r");
if (!$fp) {
echo "<br>Hit Counter: Error opening file for reading.<br>";
}
else {
$visits = 0;
while (!feof ($fp)) {
$line = fgets($fp, 4096);
$visits++;
//echo $buffer;
}
fclose ($fp);
// Display visits count
echo $visits;
}
}
log_visit();
show_visit();
?>