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

#1 2011-10-30 01:06:53

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

Customisable PHP View Counter

That's right! I made a neat little hit counter that can be easily adapted to fit just about anywhere! Forum topics, forum posts, websites, etc. Here's an example for this forum topic:

http://www.code-share.tk/viewcount/viewcount.php?url=http%3A%2F%2Fscratch.mit.edu%2Fforums%2Fviewtopic.php%3Fid%3D79239

Last edited by ohaiderstudios (2011-10-30 20:13:42)


Fork Clamor on GitHub!

Offline

 

#2 2011-10-30 03:19:29

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Customisable PHP View Counter

Eh, that doesn't work for me? Is it ment to say 1 view?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#3 2011-10-30 04:09:21

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Customisable PHP View Counter

well it says one view for me as well, so I'm assuming it doesn't work.


http://i.imgur.com/zeIZW.png

Offline

 

#4 2011-10-30 05:23:34

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

Re: Customisable PHP View Counter

Yeah, this does not appear to be incrementing. If you post your code I or rookwood should be happy to make suggestions  smile


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

Offline

 

#5 2011-10-30 05:33:11

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Customisable PHP View Counter

I'm also working on a counter like that for my website  wink  Maybe I could help  smile


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#6 2011-10-30 05:50:03

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

Re: Customisable PHP View Counter

Assuming you have a database called viewcounter with the following structure:

------------------------------
|    url     |  count   |
------------------------------
|http://...|    55      |
------------------------------

and you access the page with a GET value for URL like so:

http://www.code-share.tk/viewcount/viewcount.php?url=http://...

Code:

<?php
mysql_connect("HOST", "USERNAME", "PASSWORD") or die(mysql_error());
                mysql_select_db("DATABASE_NAME") or die(mysql_error());

$query = "UPDATE `viewcounter` SET `count` = `count`+1 WHERE `url` = '$_GET[url]' ";
mysql_query($query);
?>

will update the counter for you, though you will still need to pull this again to get it on your image.

You may notice that during the transfer, some of the characters in your URL have been converted to % symbols, make sure this isn't messing with your code.

Last edited by sparks (2011-10-30 05:50:50)


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

Offline

 

#7 2011-10-30 11:48:53

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

Re: Customisable PHP View Counter

Alrighty...so yes, apparently it doesn't work. I'm not using databases, I'm using text files...I'll post the code in a sec.


Fork Clamor on GitHub!

Offline

 

#8 2011-10-30 11:51:14

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

Re: Customisable PHP View Counter

Here's the code:

Code:

<?php
// Check my newer post...it has the working code!
?>

It's probably something obvious that I missed or didn't know about...

Last edited by ohaiderstudios (2011-10-31 18:59:18)


Fork Clamor on GitHub!

Offline

 

#9 2011-10-30 12:02:08

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Customisable PHP View Counter

one thing it could be, is that in the url for your image, you have to put in characters like : and / and that could be screwing it up.


http://i.imgur.com/zeIZW.png

Offline

 

#10 2011-10-30 12:09:06

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

Re: Customisable PHP View Counter

rookwood101 wrote:

one thing it could be, is that in the url for your image, you have to put in characters like : and / and that could be screwing it up.

No...I took precautions against that...the url for this topic would become this:
http%3A%2F%2Fscratch.mit.edu%2Fforums%2Fviewtopic.php%3Fid%3D79239
So there wouldn't be any confusion.


Fork Clamor on GitHub!

Offline

 

#11 2011-10-30 13:52:17

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

Re: Customisable PHP View Counter

Right, well I have no experience with files. There's a lot of problems with files, if two people try to access it at once there will be errors or overwrites so it's not the best way to do it.


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

Offline

 

#12 2011-10-30 15:04:48

videogame9_test
Scratcher
Registered: 2011-10-17
Posts: 43

Re: Customisable PHP View Counter

I'm the first viewer!

...wait.

Offline

 

#13 2011-10-30 15:34:46

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

Re: Customisable PHP View Counter

videogame9_test wrote:

I'm the first viewer!

...wait.

big_smile


Fork Clamor on GitHub!

Offline

 

#14 2011-10-30 15:41:01

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Customisable PHP View Counter

Using MySQL is a LOT more secure  wink


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#15 2011-10-30 20:12:23

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

Re: Customisable PHP View Counter

WindowsExplorer wrote:

Using MySQL is a LOT more secure  wink

Yes...but sometimes its easier to use text files...but then again, in some ways its harder...


Fork Clamor on GitHub!

Offline

 

#16 2011-10-30 23:20:57

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

Re: Customisable PHP View Counter

I think it might be working...check it out now!


Fork Clamor on GitHub!

Offline

 

#17 2011-10-31 03:21:29

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Customisable PHP View Counter

ohaiderstudios wrote:

I think it might be working...check it out now!

is it unique views? If it is, then it's working  big_smile


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#18 2011-10-31 18:57:12

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

Re: Customisable PHP View Counter

WindowsExplorer wrote:

ohaiderstudios wrote:

I think it might be working...check it out now!

is it unique views? If it is, then it's working  big_smile

Yay!!!!!  big_smile

Yes it is unique views...here's the new code!!!!

Code:

<?php
if (isset($_GET['url'])) {
    $file = 'counters/' . md5($_GET['url']) . '.txt';
    $ip = $_SERVER['REMOTE_ADDR'];

    if (file_exists($file)) {
        if (@file_get_contents($file)) {
            $raw = file_get_contents($file);
            $data = explode(PHP_EOL, $raw);
            $views = count($data);
        } else {
            $img = imagecreate(200, 20);
            $bg = imagecolorallocate($img, 255, 255, 255);
            $fg = imagecolorallocate($img, 0, 0, 0);

            imagestring($img, 5, 5, 5, "Too many views!", $fg);

            header('Content-type: image/png');
            imagepng($img);
            imagedestroy($img);
            die;
        }
    } else {
        touch($file);
        $data = array();
        $views = 0;
    }

    if (!in_array($ip, $data)) {
        $data[$views] = $ip;
        $views++;
        $content = implode(PHP_EOL, $data);
        file_put_contents($file, $content, LOCK_EX);
    }

    if ($views == 1) {
        $txt = "1 view";
    } else {
        $txt = $views . " views";
    }

    $img = imagecreate(200, 20);
    $bg = imagecolorallocate($img, 255, 255, 255);
    $fg = imagecolorallocate($img, 0, 0, 0);

    imagestring($img, 5, 5, 5, $txt, $fg);

    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
    die;
} else {
    $img = imagecreate(200, 20);
    $bg = imagecolorallocate($img, 255, 255, 255);
    $fg = imagecolorallocate($img, 255, 0, 0);

    imagestring($img, 5, 5, 5, "ERROR: INVALID URL!", $fg);

    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
    die;
}
?>

Fork Clamor on GitHub!

Offline

 

#19 2011-10-31 19:00:06

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Customisable PHP View Counter

It's unique until the IP changes  hmm

Offline

 

#20 2011-10-31 19:11:39

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

Re: Customisable PHP View Counter

Magnie wrote:

It's unique until the IP changes  hmm

About as unique as it can get  wink


Fork Clamor on GitHub!

Offline

 

#21 2011-11-02 06:46:27

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Customisable PHP View Counter

ohaiderstudios wrote:

Magnie wrote:

It's unique until the IP changes  hmm

About as unique as it can get  wink

Unless you use host name (whatever it's called)instead  big_smile

Last edited by LS97 (2011-11-02 06:48:13)

Offline

 

Board footer