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.
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?
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.
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 :wave:
Quote:
You would put that code posted above into a php file then make another file VisitorCount.txt with the contents "0"
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?