| New Website Hitcounter With Images | |
|
Author | Message |
---|
SKiddie Member
Posts : 9 Join date : 2011-05-11
| Subject: New Website Hitcounter With Images Wed May 11, 2011 10:53 pm | |
| - Code:
-
<?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!!! |
|
| |
Understand Member
Posts : 21 Join date : 2011-03-09
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 10:54 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: - Code:
-
<?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. |
|
| |
SKiddie Member
Posts : 9 Join date : 2011-05-11
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 10:55 pm | |
| Did it works now(after the change?) |
|
| |
SKiddie Member
Posts : 9 Join date : 2011-05-11
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 10:56 pm | |
| - Understand wrote:
- 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: - Code:
-
<?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! |
|
| |
SKiddie Member
Posts : 9 Join date : 2011-05-11
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 10:56 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 |
|
| |
Twerk Member
Posts : 26 Join date : 2011-04-17
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 10:57 pm | |
| 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?? |
|
| |
Understand Member
Posts : 21 Join date : 2011-03-09
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 11:00 pm | |
| - SKiddie wrote:
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: - Code:
-
print '<img src="../IMG/' . $sTxt[$i] . '.bmp" />'; // not print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
i added this - Code:
-
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: - Code:
-
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: - Code:
-
<?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" />'; } ?>
|
|
| |
SKiddie Member
Posts : 9 Join date : 2011-05-11
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 11:01 pm | |
| - Understand wrote:
- SKiddie wrote:
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:
- Code:
-
print '<img src="../IMG/' . $sTxt[$i] . '.bmp" />'; // not print '<img src="../IMG/' . $sTxt[$i] . '.bmp' />";
i added this
- Code:
-
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:
- Code:
-
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: - Code:
-
<?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" />'; } ?>
n this case we need to use or isset() or !=1 |
|
| |
SKiddie Member
Posts : 9 Join date : 2011-05-11
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 11:01 pm | |
| Can't I Get something wrong I have only 12 Years |
|
| |
Tiger Member
Posts : 7 Join date : 2011-04-17
| Subject: Re: New Website Hitcounter With Images Wed May 11, 2011 11:02 pm | |
| - Twerk wrote:
- You don't feel hit counters and digital-style digits are outdated??
No way, every guestbook/shoutbox needs one. |
|
| |
Sponsored content
| Subject: Re: New Website Hitcounter With Images | |
| |
|
| |
| New Website Hitcounter With Images | |
|