Where to read the visitor info

前端 未结 6 911
迷失自我
迷失自我 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-03 16:08

    Hi this is what i am using to register visitors ip.

    function get_IP() {
    
        // ADRES IP
        if     (getenv('HTTP_CLIENT_IP'))       $ipaddress = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))     $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))   $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))       $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))          $ipaddress = getenv('REMOTE_ADDR');
        else                                    $ipaddress = 'UNKNOWN';
        //
        return $ipaddress;
    }
    
    0 讨论(0)
  • 2021-01-03 16:21

    Simple:

    <?php
    $cookie_name = 'counter';
    $file = 'count.txt';
    
    if (!isset($_COOKIE[$cookie_name])) {
        $count = strval(file_get_contents($file));
        file_put_contents($file, $count + 1);
        setcookie($cookie_name, "Checked", time() + 111400);
    }
    ?>
    
    0 讨论(0)
  • 2021-01-03 16:23

    You can save a bit of code by opening the file with w+, which will create it automatically for you.

    <?php
    // Inits
    $file = "/tmp/counts.html";
    $cookie_namee='mycounterr-456';
    
    // File, created if !exists
    $fh = fopen($file, 'w+');
    // Get the count, 0 if the file is empty or just created
    $count = (int)fgets($fh);
    
    //if cookie isn't already set,then increase counts 
    //by one + save ip, and set a cookie to pc...
    if (!isset($_COOKIE[$cookie_namee])) {
        // Increment and write the new count
        fwrite($fh, ++$count);
        setcookie($cookie_namee, "Checked", time() + 111400);
    }
    
    fclose($fh);
    

    If you want a really easy way to enforce counts by IP or something, you've got to check out Redis.

    0 讨论(0)
  • 2021-01-03 16:24

    If you want it to show the visitors on your page put this under your code.

    <?php
    
    include ("counts.html")
    
    ?> 
    
    0 讨论(0)
  • 2021-01-03 16:29

    You can typically get IP address via $_SERVER['REMOTE_ADDR'] variable.

    0 讨论(0)
提交回复
热议问题