Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-10-2011, 07:13 PM   PM User | #1
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
Smile New Website Hitcounter With Images

PHP 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 $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 $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!!!



These are the digits just copy all in a folder called IMG that is in the same dir as the script(the folder)
ionutica is offline   Reply With Quote
Old 05-10-2011, 07:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 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'1time()+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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
ionutica (05-10-2011)
Old 05-10-2011, 07:38 PM   PM User | #3
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
Did it works now(after the change?)
ionutica is offline   Reply With Quote
Old 05-10-2011, 07:48 PM   PM User | #4
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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 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'1time()+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 is offline   Reply With Quote
Old 05-10-2011, 08:01 PM   PM User | #5
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
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
ionutica is offline   Reply With Quote
Old 05-10-2011, 08:29 PM   PM User | #6
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by ionutica View Post
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??
__________________
ZCE

Last edited by kbluhm; 05-10-2011 at 08:31 PM..
kbluhm is offline   Reply With Quote
Old 05-10-2011, 08:31 PM   PM User | #7
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by ionutica View Post
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
oesxyl is offline   Reply With Quote
Old 05-10-2011, 11:23 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by ionutica View Post
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:
PHP Code:
print '<img src="../IMG/' $sTxt[$i] . '.bmp" />';
// not
print '<img src="../IMG/' $sTxt[$i] . '.bmp' />"; 
Quote:
Originally Posted by ionutica View Post
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:
PHP Code:
if (isset($_COOKIE['countplus']) && $_COOKIE['countplus'] != 1)
{
    ++
$txt;
    
file_put_contents("counter.txt"$txt);  
    
setcookie('countplus'1time()+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:
PHP 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.

Edit:
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 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'1time()+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" />';
}
?>

Last edited by Fou-Lu; 05-10-2011 at 11:26 PM..
Fou-Lu is offline   Reply With Quote
Old 05-11-2011, 06:06 AM   PM User | #9
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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:
PHP Code:
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:
PHP Code:
if (isset($_COOKIE['countplus']) && $_COOKIE['countplus'] != 1)
{
    ++
$txt;
    
file_put_contents("counter.txt"$txt);  
    
setcookie('countplus'1time()+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:
PHP 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.

Edit:
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 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'1time()+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 is offline   Reply With Quote
Old 05-11-2011, 06:10 AM   PM User | #10
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
Can't I Get something wrong I have only 12 Years
ionutica is offline   Reply With Quote
Old 05-11-2011, 03:19 PM   PM User | #11
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by kbluhm View Post
You don't feel hit counters and digital-style digits are outdated??
No way, every guestbook/shoutbox needs one.
Inigoesdr is offline   Reply With Quote
Old 05-11-2011, 03:31 PM   PM User | #12
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by Inigoesdr View Post
No way, every guestbook/shoutbox needs one.
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 05-11-2011, 06:48 PM   PM User | #13
ionutica
New to the CF scene

 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
ionutica is an unknown quantity at this point
Do you like it? Add to my reputation!!!
ionutica is offline   Reply With Quote
Old 09-24-2011, 10:54 AM   PM User | #14
mberila
New to the CF scene

 
Join Date: Sep 2011
Location: the matrix
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
mberila is an unknown quantity at this point
the digit images on the first post don't load
mberila is offline   Reply With Quote
Reply

Bookmarks

Tags
hit, image, img, imgs, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 AM.


Advertisement
Log in to turn off these ads.