So, I am making a little thing that counts the number of views a page gets, and I have this code:
<!DOCTYPE html>
<html>
<head>
<title>Page View Count</title>
</head>
<body bgcolor="#FFFFFF">
<?php
$file = 'viewcount.txt';
$views = file_get_contents($file);
if (!isset($_COOKIE['viewed'])) {
$views++;
file_put_contents($file, $views)
setcookie('viewed', '1', time() + (60 * 60 * 24));
}
print '<p>This page has been visited by $views people!</p>';
?>
</body>
</html>But I get a parse error saying "unexpected T_STRING on line 14!"
What am I doing wrong????
Offline
You may need a ; on the end of line 14. However, you're going about a view counter the wrong way. The cookie function stores data on the viewer's computer so it would only count the views of the users currently viewing the page and that count could only be seen by that user.
Try MySQL.
Offline
Nononononono! See, the cookie is just to prevent one person from refreshing the page and making the count go super high. Actually, if you had LOOKED AT THE CODE
you would have seen that the view counter is actually a text file, that gets overwritten every time someone new comes to the page!
Offline
Forgive me if this is stupid (I'm just getting into PHP) but how can a user on a different computer access the viewcount.txt (assuming it is on your computer). From my (very) limited knowledge/experience, I'd use a database.
Offline
well, viewcount.txt is on the server! php has access to the file, and can write to it and read it!
Offline
I know what's wrong. The file is a String. So when you set views to contents of file it becomes a string you can't add 1 to a string it doesn't make sense. You need to typecast the string to an integer. Let me see...
try:
$views = (integer)file_get_contents($file);
or
$views = (integer)(file_get_contents($file));
That will set views to a integer and you should able to add 1 to it and save it.
My site Offline
sparks wrote:
That.... is a really neat idea, actually! MySQL is great for complex things, but for simple things like that it takes waaay to much time to set something up!
not neat ... what if two people open the same file together
.. its imcapable of handling atomic operations (sad).
(he will have to write a lock for file and poll the file till its locked to avoid :this:)
say a user adam and eve open the same file together..
adam reads the buffer and the file is truncated for write .. at the same time eve opens the file , now as its truncated it wont be able to open it .. bam error is formed x) .. sometimes this curropts the file.
and his trick is known as flat - file. which actually is harder then using SQL
Offline
fanofcena wrote:
[
not neat ... what if two people open the same file together.. its imcapable of handling atomic operations (sad).
(he will have to write a lock for file and poll the file till its locked to avoid :this:
can the server handle multiple requests at the SAME time?
Offline
fanofcena wrote:
sparks wrote:
That.... is a really neat idea, actually! MySQL is great for complex things, but for simple things like that it takes waaay to much time to set something up!
not neat ... what if two people open the same file together
.. its imcapable of handling atomic operations (sad).
(he will have to write a lock for file and poll the file till its locked to avoid :this:)
say a user adam and eve open the same file together..
adam reads the buffer and the file is truncated for write .. at the same time eve opens the file , now as its truncated it wont be able to open it .. bam error is formed x) .. sometimes this curropts the file.
and his trick is known as flat - file. which actually is harder then using SQL
Yes, although I forgot it in the small snippet of code I wrote above, I will use the LOCK_EX to lock the file exclusively to the executing script while the file is being overwritten, so no, although it can't handle atomic operations, no errors will be formed!
Offline