/    Sign up×
Community /Pin to ProfileBookmark

backup the counter into a file

hello everyone,
Atm I’m trying to make a counter reseter work, but no luck.
Here is content of the reset_counter.php:

[code=php]<?php
$reset = “0”;
$file = “counter.txt”;
$handle = fopen ($file, “r”);
$contents = fread ($handle, filesize ($file));
fclose ($handle);
print “Total number of views: <b>$contents</b>”;
$date = date(‘h:i:s’);
$translate = ‘echo $date | tr : .’;
$command = ‘echo $contents > backup/counter_$translate.txt’;
system(“$translate”);
exec(“$command”);
$fp = fopen($file,”w”);
fwrite($fp,$reset);
fclose($fp);
?>[/code]

I tried to allow the safe exec dir in htaccess, but then it gave me error 500.
Any straight answers will be appreciated.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 19.2011 — It would, in my opinion, be both simpler and safer to use PHP's file functions instead of system() commands.
[code=php]
<?php
$reset = "0";
$file = "counter.txt";
$contents = trim(file_get_contents($file));
print "Total number of views: <b>$contents</b>";
$date = date('h.i.s'); // no need to use ":" if you want "."
file_put_contents("backup/counter_$date.txt", $contents);
file_put_contents($file, $reset);
?>
[/code]
Copy linkTweet thisAlerts:
@Adolf1994authorMay 24.2011 — oh yeah, I found out, those system and exec calls can make a way to hackers to do something nasty.

So I looked for alternatives, like this:
[code=php]<?php
$reset = "0";
$file = "counter.txt";
$handle = fopen ($file, "r");
$contents = fread ($handle, filesize ($file));
fclose ($handle);
print "Total number of views: <b>$contents</b>";
$date = date('Y.m.d-h.i.s');
copy("counter.txt","backup/counter_$date.txt");
$fp = fopen($file,"w");
fwrite($fp,$reset);
fclose($fp);
?>[/code]


but yours is smaller, so thanks for it ?
×

Success!

Help @Adolf1994 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...