Where to read the visitor info

前端 未结 6 1028
迷失自我
迷失自我 2021-01-03 15:29

UPDATE:

This question exposed the obsolete, worst approach for visitors count and it should be avoided by everyone out there. Use sophis

6条回答
  •  猫巷女王i
    2021-01-03 16:05

    Since I didn't find a satisfying "simple enough" solution, I came up with my own. Create an empty file called ip.txt and use this somewhere in your code:

    $ip_all = file("ip.txt");
    $ip_cur = $_SERVER['REMOTE_ADDR']."\n";
    if (!in_array($ip_cur, $ip_all)) {
        $ip_all[] = $ip_cur;
        file_put_contents("ip.txt", implode($ip_all));
    }
    
    echo "visitors: " . count($ip_all);
    

    Note that this file will can get somewhat large over time depending on the amount of visitors you get since the entries don't expire and get deleted like cookies. But as already mentioned, I wanted it to be as simple as possible and don't care about that. Also I don't want to rely on cookies because I doubt web-crawlers and other robots will send them back.

提交回复
热议问题