Unstoppable Domains

Emptying a text file via Php every 24 hours?

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Hey,

I'm looking to use a hit counter on a site of mine and was wondering if it is possible to clear the contents of the .txt via phpe very 24 hours?

The code I am using is:

PHP:
<?php

$filename = "counter.txt"; 
$handle   = fopen($filename, "r"); 
$views    = fread($handle, filesize($filename));
fclose($handle); 

$handle   = fopen($filename, "w"); 
$views++; //Adds 1 hit
fwrite($handle, $views); 
fclose($handle);

echo $views; 

?>

Any help would be great :tu:
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Something like..

PHP:
$curtime = getdate();

if ($curtime['hours'] == 0 AND $curtime['minutes'] == 0) // midnight
{
	$handle = fopen($filename, "w");
	fwrite($handle, '0');
	fclose($handle);	
}

EDIT: thinking about it.. Matt has the better idea ;)
 
0
•••
edit: SecondVersion just beat me with another method :hehe:

SecondVersion, would that not have to be run every minute though?

---------------------------

The first thing you want is the actual PHP to clear the file. I'm a little out of practice if I'm honest but i know no better way to clear a file than to:

1) Rewrite it with nothing in
2) Delete (unlink), Recreate, CHMOD

Since the first of the options is easier and less dependent on server permissions we'll do that ;)

PHP:
<?php

// I prefer using file_put_contents, so just a suggestion and of course make it backwards compatible with < php5.
// You could cut this down if you want.
if ( !function_exists('file_put_contents') )
{
	function file_put_contents($filename, $data) 
	{
		
		$link = @fopen($filename, 'w');
		if( !$link ) 
		{ 
			return false; 
		}
			
	    fwrite($filename, $data);
	    fclose($filename);
	    return true;
	}
}

file_put_contents('counter.txt', '0');

?>
(Note: Not done any programming in a while and is untested)

Now you need a cron job to run the file every 24 hours. Cron jobs can be a bitch to setup however CPanel has a very nice interface to do it with so your best method of a attack is, as ever:

http://www.google.com/search?hl=en&safe=off&q=cpanel+cron+job+tutorial&btnG=Search

The command you want will be something like this:
Code:
php /home/**your username**/public_html/cron.php

Hope that helps.

Matt
 
Last edited:
1
•••
I see there has been some methods added. The following was my aproach without having to fiddle with cron jobs.

PHP:
<?php
$today = date("z");
$filename = "counter.txt"; 
$handle   = fopen($filename, "r"); 
$content  = fread($handle, filesize($filename));
$content_ary = explode(",", $content);
$views = $content_ary[1];
fclose($handle); 

$handle   = fopen($filename, "w"); 
if ($content_ary[0] == $today){
   $views++; //Adds 1 hit
} else {
   $views = 1;
}
$new_content = $today.",".$views;
fwrite($handle, $new_content); 
fclose($handle);

echo $views; 

?>
 
1
•••
rep left to all, going to use paaaaaaaaaaa's approach

Thanks to all <3
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back