Reputation: 565
I want to track which sites are using my theme. And I added a code to my theme which is <img src="http://example.com/callback.php">
and my callback.php file content is
<?php
if(!$_SERVER['HTTP_REFERER']){
echo "No direct access!";
} else {
$logfile= 'log.txt';
if(is_writable($logfile)) {
$referer = parse_url($_SERVER['HTTP_REFERER']);
$referer = $referer['host'];
$fp = fopen($logfile, "a");
fwrite($fp, $referer);
fwrite($fp, "\n");
fclose($fp);
} else {
echo "log.txt is not writable";
}
}
?>
But it writes same domain every page load. I want to check domain name first and if domain name is in the text file it should not add it.
Upvotes: 0
Views: 70
Reputation: 272
Try this code:
<?php
if(!$_SERVER['HTTP_REFERER']){
echo "No direct access!";
exit();
} else {
$logfile= 'log.txt';
if(is_writable($logfile)) {
$referer = parse_url($_SERVER['HTTP_REFERER']);
$referer = $referer['host'];
$fp = fopen($logfile, "a+");
$flag=false;
while(!feof($fp)){
if(trim(fgets($fp)) === trim($referer))
$flag=true;
}
if(!$flag){
fwrite($fp, $referer);
fwrite($fp, "\r\n");
}
fclose($fp);
} else {
echo "log.txt is not writable";
exit();
}
}
?>
Upvotes: 1
Reputation: 9582
Try this:
<?php
if (!$_SERVER['HTTP_REFERER']) {
echo "No direct access!";
exit();
}
$logfile= 'log.txt';
if (!is_writable($logfile)) {
echo "log.txt is not writable";
exit();
}
$referer = parse_url($_SERVER['HTTP_REFERER']);
$host = $referer['host'];
$hosts = file($logfile, FILE_IGNORE_NEW_LINES);
if (in_array($host, $hosts)) {
exit();
}
$handle = fopen($logfile, "a");
fwrite($handle, $host);
fwrite($handle, "\n");
fclose($handle);
For reference, see:
Upvotes: 1