|
-
May 2nd, 2005, 05:40 PM
#1
Thread Starter
Admodistrator
Can you explain this?
Okay, i found this good php script:
PHP Code:
<?php
$countfilename = "VisitorCount.txt";
$whattoread = @fopen($countfilename, "r") or die("Error opening count file");
$contents = fread($whattoread, filesize($countfilename));
fclose($whattoread);
$contents = ($contents + 1);
$whattoread = @fopen($countfilename, "w") or die("Error opening count file1");
@fwrite($whattoread, $contents) or die("Error with addition");
fclose($whattoread);
echo $contents;
?>
works like a charm..great job..what im wondering is if i can have an explanation for what it all does?
you dont have to explain like in depth, just like $countfilename, and all those and there arguments...anything you see fit..
thanks alot 
**edit
im beginning to understand alot, this isnt so hard, at all!!
heres what ive figured out in the last 5 minutes:
Everything with a $ is a string, not a fixed variable(what i mean is, to open a file i dont have to type $whattoread =, it can be $readfile =
$whattoread = @fopen($filename, "r") or die("Error opening count file");
means that it should "r"ead the file, on error say ("Error opening count file")--dont really understand what die does though
and that you can add to strings: echo $contents, " Visitors"
other help = good
Last edited by visualAd; May 2nd, 2005 at 06:05 PM.
Reason: bizarre request :\
-
May 2nd, 2005, 06:19 PM
#2
Re: Can you explain this?
It looks to me like a hit counter. All variables are prefixed with a $ symbol.
Line 1 assigns the string VisitorCount.txt to the variable $countfilename
Line 2 opens the file in $countfilename for reading and assigns the returned resource to the variable $whattoread
Line 3 assigns the content of the file in $whattoread to the varaible $contents as string.
Line 4 closes the file in $whattoread
Line 5 type casts the variable $contents to an integer and adds one to it
Line 6 again opens the file in the variable $countfilename but this time for wirting and assigns its resource to the variable $whattoread
Line 7 the contents of the variable $contents is type casted to a string and written to the file
Line 8 the file in in $whattoread it closed
Line 9 the contents of the variable $contents are sent to the output stream. Usually the browser.
-
May 2nd, 2005, 06:23 PM
#3
Thread Starter
Admodistrator
Re: Can you explain this?
cool, great post and very helpful
Can you explain to me how id save to a website the counter? maybe thru ftp or something?
-
May 2nd, 2005, 11:11 PM
#4
Frenzied Member
Re: Can you explain this?
You would put that code posted above into a php file then make another file VisitorCount.txt with the contents "0" and chmod it to 777. If you then open the php file through the webserver it will say 1 hit. That code could be much easier though:
PHP Code:
if(!function_exists('file_put_contents')) {
function file_put_contents($filename, $data, $file_append = false) {
$fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
if(!$fp) {
trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
return;
}
fputs($fp, $data);
fclose($fp);
}
}
$Counter=file_get_contents("VisitorCount.txt");
$Counter++;
file_put_contents("VisitorCount.txt",$Counter);
echo $Counter;
I guess it's not that much shorter since you have to declare the function unless you're using PHP 5 but I have that function in must of my includes anyways. Either way it should work.
Last edited by numtel; May 2nd, 2005 at 11:14 PM.
-
May 3rd, 2005, 03:54 PM
#5
Thread Starter
Admodistrator
Re: Can you explain this?
i honestly understood none of your code...could you explain?
btw, i understand how to use the code, lol thats the easy part
You would put that code posted above into a php file then make another file VisitorCount.txt with the contents "0"
-
May 3rd, 2005, 04:25 PM
#6
Thread Starter
Admodistrator
Re: Can you explain this?
okay, i uploaded that file to a server(the one in my first post) and im getting a write error. I assume its because it does not have access to the file..any ideas?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|