-1
$ip = $_SERVER['REMOTE_ADDR'];                       //get IP
$referer = $_SERVER['HTTP_REFERER'];                 //get referer
$time = time();                                      //extracting timestamp
$date = date('d-m-y H:i:s',$time);                   //generating date
$f = fopen("log.php", "a");                          //opening files
$counter = fgetc("hitleap.txt");
$f2 = fopen("hitleap.txt", "w+");

if($referer == "http://hitleap.com/traffic-exchange"){
fwrite($f2, $counter++);                             
fclose($f2);
} else { 
fwrite ($f, '<div class="spoiler"><input type="button" onclick="showSpoiler(this)" value="Show/Hide" />
Data/Ora: <b>['.$date.']</b><br>
<div class="inner" style="display:none">
IP: <b>['.$ip.']</b> <br>
Provenienza: <b>['.$referer.']</b>
</div></div><br>



 ');
fclose($f);
}

Basically if the site visitor is coming from hitleap site, I want to increment a number inside a txt file by 1. If not, add into another log file his IP and referer inside a spoiler. The second part works pretty well but I can't still get the first part to work

Also the hitleap.txt somehow gets flushed everytime the "else" applies.

EDIT Solved like this:

$ip = $_SERVER['REMOTE_ADDR'];
$referer = $_SERVER['HTTP_REFERER'];
$time = time();
$date = date('d-m-y H:i:s',$time);
$hits = file_get_contents("hitleap.txt");
$nhits = $hits+1;

if($referer == "http://XXX"){
$f2 = fopen("hitleap.txt", "w+");
fwrite($f2, $nhits);
fclose($f2);
} else {
$f = fopen("log.php", "a");
fwrite ($f, 'wh@tever u want here');
fclose($f);
}

seems to work fine for me

Walt9
  • 5
  • 2
  • `fwrite($f2, $counter++);` at first it writes the value and than it increments the `$counter`. write it as `fwrite($f2, ++$counter);` – Cheery Oct 11 '14 at 23:31
  • 1
    $f2 = fopen("hitleap.txt", "w+"); - This will open file hitleap.txt in write mode, after deleting its previous contents. It should work fine if you put this line inside the if block – Ananth Oct 11 '14 at 23:31
  • Sidenote about `$_SERVER['HTTP_REFERER']` => http://stackoverflow.com/a/6023980/ – Funk Forty Niner Oct 11 '14 at 23:45

2 Answers2

2

You'll wanna use

fwrite($f2, ++$counter);

instead of

fwrite($f2, $counter++); 

What's happening is that your incrementing the variable after it's been written to file and not before. The solution will increment the variable before writing it.

Kakuriyo S
  • 51
  • 2
1

Your final code would be:

$ip = $_SERVER['REMOTE_ADDR'];                       //get IP
$referer = $_SERVER['HTTP_REFERER'];                 //get referer
$time = time();                                      //extracting timestamp
$date = date('d-m-y H:i:s',$time);                   //generating date
$f = fopen("log.php", "a");                          //opening files
$counter = fgetc("hitleap.txt");

// this line has been moved into the if block

if($referer == "http://hitleap.com/traffic-exchange"){
$f2 = fopen("hitleap.txt", "w+"); // here it is
fwrite($f2, ++$counter);                       // using ++$counter      
fclose($f2);
} else { 
fwrite ($f, '<div class="spoiler"><input type="button" onclick="showSpoiler(this)" value="Show/Hide" />
Data/Ora: <b>['.$date.']</b><br>
<div class="inner" style="display:none">
IP: <b>['.$ip.']</b> <br>
Provenienza: <b>['.$referer.']</b>
</div></div><br>



 ');
fclose($f);
}
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • Tried this one out. The hitleap.txt file still gets flushed instead of its number incremented. – Walt9 Oct 12 '14 at 08:55