1

UPDATE:

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

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • Have a look at the $_SERVER variable, it can give you the IP and several other bits of info if you need it. http://php.net/manual/en/reserved.variables.server.php – azzy81 Mar 28 '13 at 17:00
  • possible duplicate of [How to get Client IP address in PHP?](http://stackoverflow.com/questions/3003145/how-to-get-client-ip-address-in-php) – John Dvorak Mar 30 '13 at 17:21

6 Answers6

4

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

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
4

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.

deathangel
  • 319
  • 2
  • 13
1

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);
}
?>
Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
1

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

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.

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
methai
  • 8,835
  • 1
  • 22
  • 21
-3

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

<?php

include ("counts.html")

?> 
Searock
  • 6,278
  • 11
  • 62
  • 98
huaa
  • 1