Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
Hi Couch!
I want to use PHP code for counting unique visitors.
Unfortunately it does not work if it is surrounded by <cms:php> ... </cms:php>
This, however, need to be able to use <cms:show k_template_id /> to create text files for each page:

Original:
Code: Select all
$lf_name = "hits.txt";

Mode:
Code: Select all
$lf_name = "<cms:show k_template_id />.txt";

However, this does not work ...

Here is the entire code:
Code: Select all
<?php

$lf_name = "hits.txt";
$monthly = 0;
$monthly_path = "oldfiles";
$type = 1;
$beforeTotalText = "Hits: ";
$beforeUniqueText = "Unique Visits: ";
$display = 1;
$separator = "<br \>";
$log_file = dirname(__FILE__) . '/' . $lf_name;

if ($_GET['display'] == "true") {
die("<pre>&#60;? include(\"" . dirname(__FILE__) . '/' . basename(__FILE__) . "\"); ?&#62;</pre>");

} else {
$uIP = $_SERVER['REMOTE_ADDR'];
if (file_exists($log_file)) {
$log = file_get_contents($log_file);
if ($monthly) {   }
else {
if ($type == 0) {
$toWrite = intval($log) + 1;
$info = $beforeTotalText . $toWrite;
} else if ($type == 1) {
$hits = reset(explode(";", $log));
$IPs = end(explode(";", $log));
$IPArray = explode(",", $IPs);
if (array_search($uIP, $IPArray, true) === false) {
$hits = intval($hits) + 1;
$toWrite = $hits . ";" . $IPs . $uIP . ",";
} else { $toWrite = $log; }
$info = $beforeUniqueText . $hits;

} else if ($type == 2) {
// Separate log file into regular hits, unique hits, and IPs
$pieces = explode(";", $log);
$totalHits = $pieces[0];
$uniqueHits = $pieces[1];
$IPs = $pieces[2];
$IPArray = explode(",", $IPs);
$totalHits = intval($totalHits) + 1;
if (array_search($uIP, $IPArray, true) === false) {
$uniqueHits = intval($uniqueHits) + 1;
$toWrite = $totalHits . ";" . $uniqueHits . ";" . $IPs . $uIP . ",";
} else { $toWrite = $totalHits . ";" . $uniqueHits . ";" . $IPs; }
$info = $beforeTotalText . $totalHits . $separator . $beforeUniqueText . $uniqueHits;
}
write_logfile($toWrite, $info);
}
} else {
$fp = fopen($log_file, "w");
fclose($fp);
if ($type == 0) {
$toWrite = "1";
$info = $beforeTotxalText . "1";
} else if ($type == 1) {
$toWrite = "1;" . $uIP . ",";
$info = $beforeUniqueText . "1";
} else if ($type == 2) {
$toWrite = "1;1;" . $uIP . ",";
$info = $beforeTotalText . "1" . $separator . $beforeUniqueText . "1";
}
write_logfile($toWrite, $info);
}
}
function write_logfile($data, $output) {
global $log_file;
file_put_contents($log_file, $data);
if ($display == 1) { echo $output; }
}
?>


Can you find a solution?
Regards
It's probably a permission issue with fopen and write.

Somewhere in the forum i readed that on the page, first the native php is executed, then the couch tags and then the cms:php tags, so probably cms:php tags are executed as the apache user instead of php/root.
Try to use the cms:php tags and see if the php/apache error log shows something.
Hi @orbital,

You need to make the following correction and the script should work even with <cms:php> -
Please change the following line -
Code: Select all
$log_file = dirname(__FILE__) . '/' . $lf_name;

to make it the following -
Code: Select all
global $log_file;
$log_file = K_SITE_DIR . $lf_name;

explaining the required changes -
Variables within cms:php need to be declared as 'global' if they are to be used elsewhere in the script. In your code, the $log_file variable was used by the 'write_logfile' function which expected a global $log_file variable and could not find it - hence the script did not work.

The second change was to give a fixed path to the log file by using K_SITE_DIR - this is because since now the PHP code is being run by Couch, the dirname(__FILE__) was evaluating to the 'couch' folder. Now the path is fixed to be the site's root.

You can now use the Couch variables within the script as planned.

Hope it helps.
3 posts Page 1 of 1