Where to put PHP code that takes information from the user's machine?

0

I have created some functions responsible for getting the IP of the user (and ISP, parents, browser, OS) in order to keep a control of who accesses, how many times he accessed in a minute but do not know where to insert in the content pages of the site. Another question is the best way to get these kinds of information?

Code that takes information

<?php
    class ClientData{

        public $ip;
        public $isp;
        public $browser;
        public $so;
        public $city;
        public $country;
        public $referrer;

        /**
        *StartCatchData() 
        *Este método tem o objetivo de realizar todas as chamadas a métodos, obtendo 
        *assim todos os dados dos cliente.
        */
        public function StartCatchData(){
            $this->CatchIP();
        }

        public function CatchIP(){
            if(array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
                $this->ip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
            }else if(array_key_exists('REMOTE_ADDR', $_SERVER)) { 
                $this->ip = $_SERVER["REMOTE_ADDR"]; 
            }else if(array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
                $this->ip = $_SERVER["HTTP_CLIENT_IP"]; 
            }else{
                $this->ip = false;
            }
        }   
    }
?>

Content page

<html>
<head>
    <title>
        Teste
    </title>
</head>
<body>
<?php
    require_once 'ClientData.php';

    $clientData = new ClientData();

    $clientData->StartCatchData();
?>
Hello World!
</body> de exemplo:
    
asked by anonymous 22.04.2015 / 17:06

1 answer

5

Your doubt is not very clear, but if I understand you have two possible "environments":

  

I recommend using include, require, etc. before anything, to keep the project organization

  • Before rendering:

    Run before the page is completely delivered to the client, because your system needs to have the same data as the client can see anything on the page.

    <?php
        require_once 'ClientData.php';
    
        $clientData = new ClientData();
        $clientData->StartCatchData();
    ?><html>
    <head>
    ...
    
  • After submitting the entire content

    This process should be done if you need to know that the client has already received the whole page, for example if you are creating a download counter, the counter should only "add" when the download is complete:

    <?php
        require_once 'ClientData.php';
    ?><html>
    
    ...
    
    </html><?php
        $clientData = new ClientData();
        $clientData->StartCatchData();
    ?>
    
  • 22.04.2015 / 17:30