PDA

View Full Version : New Website Hitcounter With Images


ionutica
05-10-2011, 07:13 PM
<?php
if($_COOKIE["countplus"] != 1)
{
$txt = file_get_contents("counter.txt");
$txt++;
file_put_contents("counter.txt", $txt);
$txt = file_get_contents("counter.txt");
$l = strlen($txt);
$nrofo = 6 - $l;

for ($i = 0; $i <= $nrofo; $i++) {
echo "<img src='../IMG/0.bmp'>";
}
for ($i = 0; $i <= $l - 1; $i++) {
echo "<img src='../IMG/" . $txt[$i] . ".bmp'>";
}
setcookie("countplus", "1", time()+60*60*24);
} else
{
$txt = file_get_contents("counter.txt");
$l = strlen($txt);
$nrofo = 6 - $l;

for ($i = 0; $i <= $nrofo; $i++) {
echo "<img src='../IMG/0.bmp'>";
}
for ($i = 0; $i <= $l - 1; $i++) {
echo "<img src='../IMG/" . $txt[$i] . ".bmp'>";
}

}
?>



This code is 100% working!!!

http://ionut.net63.net/IMG/0.bmp http://ionut.net63.net/IMG/1.bmp http://ionut.net63.net/IMG/2.bmp http://ionut.net63.net/IMG/3.bmp http://ionut.net63.net/IMG/4.bmp http://ionut.net63.net/IMG/5.bmp http://ionut.net63.net/IMG/6.bmp http://ionut.net63.net/IMG/7.bmp http://ionut.net63.net/IMG/8.bmp http://ionut.net63.net/IMG/9.bmp

These are the digits just copy all in a folder called IMG that is in the same dir as the script(the folder)

Fou-Lu
05-10-2011, 07:35 PM
This would actually belong in the snippets forum.
There is too much work happening in here. The code is replicated for both the cookie and non cookied approaches. This can be factored down to this:

<?php

$iPad = 10;
$txt = file_get_contents("counter.txt");

if (isset($_COOKIE['countplus']) && $_COOKIE['countplus'] != 1)
{
++$txt;
file_put_contents("counter.txt", $txt);
setcookie('countplus', 1, time()+86400);
}

$sTxt = str_pad($txt, $iPad, '0', STR_PAD_LEFT);
$iTxtLength = strlen($sTxt);
for ($i = 0; $i < $iTxtLength; ++$i)
{
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
}
?>


I didn't do anything to trap errors or change the file handling.

I would go a completely different way and use the GD to overlay the images together and kick out a script that is served as an image. The pro is that you have one image, the con is that it takes more work to do.

ionutica
05-10-2011, 07:38 PM
Did it works now(after the change?)

ionutica
05-10-2011, 07:48 PM
This would actually belong in the snippets forum.
There is too much work happening in here. The code is replicated for both the cookie and non cookied approaches. This can be factored down to this:

<?php

$iPad = 10;
$txt = file_get_contents("counter.txt");

if (isset($_COOKIE['countplus']) && $_COOKIE['countplus'] != 1)
{
++$txt;
file_put_contents("counter.txt", $txt);
setcookie('countplus', 1, time()+86400);
}

$sTxt = str_pad($txt, $iPad, '0', STR_PAD_LEFT);
$iTxtLength = strlen($sTxt);
for ($i = 0; $i < $iTxtLength; ++$i)
{
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
}
?>


I didn't do anything to trap errors or change the file handling.

I would go a completely different way and use the GD to overlay the images together and kick out a script that is served as an image. The pro is that you have one image, the con is that it takes more work to do.
Oh, here is a error
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
use
echo "<img src='../IMG/$sTxt[$i].bmp' />";
print is outdated!

ionutica
05-10-2011, 08:01 PM
And you got the part with isset because the cookie will be inexistent or = to 1
so it will never be equal to 2 or 3 or 0 no it will be equal to "" not set but != 1

kbluhm
05-10-2011, 08:29 PM
Oh, here is a error
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
use
echo "<img src='../IMG/$sTxt[$i].bmp' />";
print is outdated!
The original was entirely fine... nothing wrong with using print(), it's perfectly acceptable.

You don't feel hit counters and digital-style digits are outdated??

oesxyl
05-10-2011, 08:31 PM
Oh, here is a error
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
use
echo "<img src='../IMG/$sTxt[$i].bmp' />";
print is outdated!
what do you mean?

best regards

Fou-Lu
05-10-2011, 11:23 PM
Oh, here is a error
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
use
echo "<img src='../IMG/$sTxt[$i].bmp' />";
print is outdated!

print will never be deprecated as its linked to the core C printf. Since it is a language construct, it would be a tremendous overhaul to remove it. And we know that's not going anywhere. Though there is an error, I have the ending ' and " in the wrong order there, that should have been:

print '<img src="../IMG/' . $sTxt[$i] . '.bmp" />';
// not
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";


And you got the part with isset because the cookie will be inexistent or = to 1
so it will never be equal to 2 or 3 or 0 no it will be equal to "" not set but != 1

I haven't a clue what you are talking about here. I added this:

if (isset($_COOKIE['countplus']) && $_COOKIE['countplus'] != 1)
{
++$txt;
file_put_contents("counter.txt", $txt);
setcookie('countplus', 1, time()+86400);
}

as my impression of what the intent was is to only count up if there is no cookie currently set.
Oh wait, I see. That is backwards, I originally wrote it thinking you were only counting if countplus was available, but realized that it was the other way around. That should be:

if (!isset($_COOKIE['countplus']) || $_COOKIE['countplus'] != 1)

This way if there is no cookie OR if there is a cookie and its value is not 1, that it will increment the counter and set the cookie.


I should probably put the end in. I'd expect that this will work, but with most of what I do it is untested:

<?php

$iPad = 10;
$txt = file_get_contents("counter.txt");

if (!isset($_COOKIE['countplus']) || $_COOKIE['countplus'] != 1)
{
++$txt;
file_put_contents("counter.txt", $txt);
setcookie('countplus', 1, time()+86400);
}

$sTxt = str_pad($txt, $iPad, '0', STR_PAD_LEFT);
$iTxtLength = strlen($sTxt);
for ($i = 0; $i < $iTxtLength; ++$i)
{
print '<img src="../IMG/' . $sTxt[$i] . '.bmp" />';
}
?>

ionutica
05-11-2011, 06:06 AM
print will never be deprecated as its linked to the core C printf. Since it is a language construct, it would be a tremendous overhaul to remove it. And we know that's not going anywhere. Though there is an error, I have the ending ' and " in the wrong order there, that should have been:

print '<img src="../IMG/' . $sTxt[$i] . '.bmp" />';
// not
print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";




I haven't a clue what you are talking about here. I added this:

if (isset($_COOKIE['countplus']) && $_COOKIE['countplus'] != 1)
{
++$txt;
file_put_contents("counter.txt", $txt);
setcookie('countplus', 1, time()+86400);
}

as my impression of what the intent was is to only count up if there is no cookie currently set.
Oh wait, I see. That is backwards, I originally wrote it thinking you were only counting if countplus was available, but realized that it was the other way around. That should be:

if (!isset($_COOKIE['countplus']) || $_COOKIE['countplus'] != 1)

This way if there is no cookie OR if there is a cookie and its value is not 1, that it will increment the counter and set the cookie.


I should probably put the end in. I'd expect that this will work, but with most of what I do it is untested:

<?php

$iPad = 10;
$txt = file_get_contents("counter.txt");

if (!isset($_COOKIE['countplus']) || $_COOKIE['countplus'] != 1)
{
++$txt;
file_put_contents("counter.txt", $txt);
setcookie('countplus', 1, time()+86400);
}

$sTxt = str_pad($txt, $iPad, '0', STR_PAD_LEFT);
$iTxtLength = strlen($sTxt);
for ($i = 0; $i < $iTxtLength; ++$i)
{
print '<img src="../IMG/' . $sTxt[$i] . '.bmp" />';
}
?>


In this case we need to use or isset() or !=1

ionutica
05-11-2011, 06:10 AM
Can't I Get something wrong I have only 12 Years

Inigoesdr
05-11-2011, 03:19 PM
You don't feel hit counters and digital-style digits are outdated??

No way, every guestbook/shoutbox needs one.

kbluhm
05-11-2011, 03:31 PM
No way, every guestbook/shoutbox needs one.

:D :thumbsup:

ionutica
05-11-2011, 06:48 PM
:mad: Do you like it? Add to my reputation!!!

mberila
09-24-2011, 10:54 AM
the digit images on the first post don't load