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:
Last edited by ohaiderstudios (2011-10-30 20:13:42)
Offline
well it says one view for me as well, so I'm assuming it doesn't work.
Offline
I'm also working on a counter like that for my website
Maybe I could help
Offline
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://...
<?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)
Offline
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.
Offline
Here's the 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)
Offline
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.
Offline
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.
Offline
I'm the first viewer!
...wait.
Offline
videogame9_test wrote:
I'm the first viewer!
...wait.
Offline
WindowsExplorer wrote:
Using MySQL is a LOT more secure
![]()
Yes...but sometimes its easier to use text files...but then again, in some ways its harder...
Offline
I think it might be working...check it out now!
Offline
ohaiderstudios wrote:
I think it might be working...check it out now!
is it unique views? If it is, then it's working
Offline
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
![]()
Yay!!!!!
Yes it is unique views...here's the new 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;
}
?>Offline
It's unique until the IP changes
Offline
Magnie wrote:
It's unique until the IP changes
![]()
About as unique as it can get
Offline
ohaiderstudios wrote:
Magnie wrote:
It's unique until the IP changes
![]()
About as unique as it can get
![]()
Unless you use host name (whatever it's called)instead
Last edited by LS97 (2011-11-02 06:48:13)
Offline