user863317
user863317

Reputation:

Can we track clients ip address with php or javascript

I don't trust any analytics or statcounter type websites I want to track my visitors by my own coding

can we track it by php or javascript if yes then how

Upvotes: 1

Views: 6164

Answers (7)

Steve
Steve

Reputation: 355

I know you could use a PHP function like this:

function getRealIpAddr(){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    //check if ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    //to check if ip is passed from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
        $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

But.., take not that obviously if a visitor is using an anonymizing service, you can't grab any IP and never you can be 100% sure about real IP in any case.

Upvotes: 0

Sobis
Sobis

Reputation: 1405

I think you cannot get it with javascript on client side. You have to execute ajax call on server and return IP from where request came to server.

You can check How to get client's IP address using javascript only?

Upvotes: 0

Sukumar
Sukumar

Reputation: 3587

You can use $_SERVER to get the client's IP address. Based on it, you can roll out your own solution.

Alternatively, you can parse your visitor logs (apache logs for example).

However, tracking visitors is not a trivial task. Eventually, you will be looking at tracking repeat visitors, unique visitors, their demographic, their machine configuration and what not. Rolling out your own solution may just be re-inventing the wheel.

Upvotes: 0

Pekka
Pekka

Reputation: 449455

can we track it by php or javascript if yes then how

Piwik is an excellent locally-hosted, PHP-driven analytics suite.

I have set it up on almost a dozen client web sites, with very good results.

Upvotes: 2

Pelshoff
Pelshoff

Reputation: 1464

You can use $_SERVER["REMOTE_ADDR"] to get the visitor's ip address. See http://www.php.net/manual/en/reserved.variables.server.php.

Upvotes: 3

Philippe Plantier
Philippe Plantier

Reputation: 8073

You may want to use your webserver's logs, then, and generate reports with something like AWstats.

Upvotes: 0

Mchl
Mchl

Reputation: 62387

See $_SERVER['REMOTE_ADDR'] but given relative ease of use of HTTP proxies, you can't really trust it either.

Upvotes: 2

Related Questions