-1

Currently, I am using this:

<A href="/?id=page_name2&referrer=<?php echo $_GET['id']; ?>">Page Name 2</A>

Which displays the URL like this:

www.domain.com/?id=page_name2&referrer=page_name

I want to store the referring data to a TXT file, which will be in a folder on a server. I do not want to use Google Analytics nor any SQL.

De1an
  • 260
  • 1
  • 14
  • So using file handling you want to handle the referrer rather to show it in the url? – Abbas Jun 29 '18 at 02:17
  • You can append every record in the text file as a new line. – hungrykoala Jun 29 '18 at 02:17
  • 1
    Possible duplicate of [How to write into a file in PHP?](https://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php) – hungrykoala Jun 29 '18 at 02:18
  • @Abbas: I still want to show it in the URL and it remains untouched, but it alone does not record any number of accessing through that specific link/referrer. – De1an Jun 29 '18 at 02:27

1 Answers1

0

Hmm, you can build a referrers.json file like this:

{
  "page_name": 1,
  "page_name_2": 4,
  "page_name_etc": 9
}

Where 1, 4 and 9 are the times those pages were used as referrers.

Look at this code:

$file = 'referrers.json';

$arr = (is_file($file))
    ? json_decode(file_get_contents($file), true)
    : [];

$referrerId = 'some_page';

if (array_key_exists($referrerId, $arr)) {
    $arr[$referrerId]++;
} else {
    $arr[$referrerId] = 1;
}

file_put_contents($file, json_encode($arr));