PHP - Create . TXT file with Visitors' IP log

Asked

Viewed 518 times

-1

I have the following link:

<a target='_blank' class='btn-primary' href='exemplo/1/'> Clique Aqui </a>

I need every time the page containing this link is accessed by a different visitor ( IP ), change the number on the link.

I think it would be interesting to create a file .txt containing the numbers of each IP and the number each IP will be responsible for.

Example ( numbers.txt ):

123.456.789.00 - 1
321.543.765.99 - 2
543.210.456.22 - 3
435.647.533.33 - 4

And so consecutively...

I need it checked if the visitor’s IP is already on that list, if he is taking the number that is responsible for this IP and puts it on the link, if he is not, then the new IP is added to the list and aggregated the number that he will be responsible for and then placed on the link, being like this:

Example: The Visitor has an IP address 321.543.765.99 who was responsible for the 2.

Then it will be displayed:

<a target='_blank' class='btn-primary' href='exemplo/2/'> Clique Aqui </a>

Is it possible to do this? How?

  • 2

    original: http://answall.com/questions/157907/fazer-bot%C3%A3o-a-alterar-href-a-cada-novo-acesso-em-determinada-p%C3%A1gina and related: http://answall.com/questions/167270/fazer-bot%C3%a3o-a-alterar-href-a-cada-novo-visitante-ip-ip-em-determinada-p%C3%a1g

  • Daniel, the question here is another friend, please understand. This question is totally different.

  • 1

    Yes.. because it didn’t say it was duplicated, but related. For others to understand the context behind it. Note also that it was not I who closed the other issue.

  • Ah yes, thank you very much Daniel for understanding! : D Vlw msm

  • 2

    so the importance in showing the originals.. Whoever takes the streetcar walking will interpret in a dispersed way.

  • 2

    As it was demonstrated in the comments of the reply that the doubts are the same of the two previous duplicates, I closed to avoid the repeated excess of information spread on the site. I suggest not opening questions with others in progress, unless the subjects are in fact different (not just different versions of the text).

Show 1 more comment

1 answer

1


A scope for how you can implement:

The txt file for testing is like this

123.456.789.00 - 1
321.543.765.99 - 2
543.210.456.22 - 3
435.647.533.33 - 4

This is the routine:

<?php

$ip = '123.123.123.123';
$file = __DIR__.DIRECTORY_SEPARATOR.'tmp6.txt';
$content = file_get_contents($file);

$number = null;
$ip_len = strlen($ip) + 3;
$pos = strpos($content, $ip);

if ($pos !== false) {
    $part1 = substr($content, $pos + $ip_len);
    //echo $part1.'<br> len '.strlen($part1);
    //var_dump(strpos($part1, PHP_EOL));
    $number = substr($part1, 0, strpos($part1, PHP_EOL));
} else {
    $pos = strrpos($content, ' ');
    //var_dump($pos);
    $number = trim(substr($content, $pos)) + 1;
    $fs = fopen($file, 'a');
    fwrite($fs, $ip.' - '.$number.PHP_EOL);
    fclose($fs);
}

echo 'number '.$number;

Be aware that it is possible to optimize and, the above script has inconsistency regarding possible failures.

The script relies on the value of PHP_EOL as a line break character. In certain environments or situations it may be incompatible. Example txt can be saved with multiple line breaks. To minimize this problem, make sure you always save with the same formatting pattern.

Still, consider using Sqlite or other logic to simplify this problem that might not even exist.

One suggestion is to just encode the IP and use this encoded string instead of the number to form the URL. In this case you do not need to do anything of this junk with txt, sqlite, nothing at all. A single line would already solve

echo md5($ip);

This is how I would solve the problem, to avoid having to give all this turn that seems unnecessary. Finally, the decision is each.

  • It looks very good! : 3 I will test now!

  • Exactly Daniel, each IP will be responsible for a number, in case when I make the first access with my IP, I will be with the number 1, if another person accesses it will be responsible for the number 2, and so on.

  • Hummm, then Daniel, these IP’s are not I who will create manually, will be created by the server itself, and each time accessed by a new IP, will be automatically edited this TXT file, inserting this new IP and its number, in case a "+1" rsrs.

  • I just opened the . TXT file that was created, and is empty!

  • is because file_put_contents overwrite... use a function that writes at the end of the file, like fwrite(), or use FILE_APPEND

  • 3

    upgrade, now add ip if not in txt

  • yeah, but now you’re adding the number to the IP instead of fixing it...

  • So, Daniel, now you’re showing off like you planned: IP: 77.543.765.930, Número: 1. It turns out that when I reload the page, it adds up to +1, getting IP: 77.543.765.930, Número: 2 and so on. He must be caught in the number he was responsible for.

  • 1

    Exactly @Shutupmagda You are also testing :D

  • 1

    I just looked at the logic @Lexandre-Lys... There’s a sum operation there...

  • 1

    So now you’re directing the question to the original which would make this one a duplicate. .. The script does what the question asks and nothing else. The rest is up to you. If there’s another rule, I’m oblivious to that

  • 2

    @Alexandrelopes, new version.. test there.. I will not edit more.

  • PERFECT!!! It was just that Daniel, you managed to solve the question! : Thank you so much for everything, just excuse me for anything, I’m new with PHP I couldn’t solve it myself. : / Anyway, thank you. Congratulations on your knowledge! P

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.