T.Todua
T.Todua

Reputation: 56351

Where to read the visitor info

UPDATE:

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

Upvotes: 1

Views: 19091

Answers (6)

Dariusz Dusza
Dariusz Dusza

Reputation: 17

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;
}

Upvotes: 1

methai
methai

Reputation: 9133

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.

Upvotes: 0

Eduardo Cuomo
Eduardo Cuomo

Reputation: 18937

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);
}
?>

Upvotes: 1

deathangel
deathangel

Reputation: 318

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.

Upvotes: 4

huaa
huaa

Reputation: 1

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

<?php

include ("counts.html")

?> 

Upvotes: -3

Mike Brant
Mike Brant

Reputation: 71384

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

Upvotes: 4

Related Questions