This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-09-05 17:55:12

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

PHP Help, Please!

So, I am making a little thing that counts the number of views a page gets, and I have this code:

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


Fork Clamor on GitHub!

Offline

 

#2 2011-09-05 18:10:45

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP Help, Please!

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.


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#3 2011-09-05 18:36:25

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: PHP Help, Please!

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  wink  you would have seen that the view counter is actually a text file, that gets overwritten every time someone new comes to the page!


Fork Clamor on GitHub!

Offline

 

#4 2011-09-05 19:16:21

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: PHP Help, Please!

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.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2011-09-05 22:17:46

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: PHP Help, Please!

well, viewcount.txt is on the server! php has access to the file, and can write to it and read it!


Fork Clamor on GitHub!

Offline

 

#6 2011-09-06 00:03:03

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: PHP Help, Please!

ohaiderstudios wrote:

well, viewcount.txt is on the server! php has access to the file, and can write to it and read it!

Sort of like MySql, but more simpler.


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#7 2011-09-06 04:14:15

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP Help, Please!

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!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#8 2011-09-06 05:03:18

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: PHP Help, Please!

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.


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#9 2011-09-06 05:52:21

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: PHP Help, Please!

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  tongue  .. 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


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#10 2011-09-06 11:19:26

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: PHP Help, Please!

fanofcena wrote:

[
not neat ... what if two people open the same file together  tongue  .. 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

 

#11 2011-09-06 22:15:37

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: PHP Help, Please!

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  tongue  .. 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!


Fork Clamor on GitHub!

Offline

 

Board footer